package Server.Poker.FJSSZ;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;

import Server.Agent.GameConfig;
import Server.Agent.GameDB;
import Server.Model.Base.IRoom;
import Server.Model.Base.IServerUserItem;
import Server.Model.Base.SHMUser;
import Server.Model.Hall.QpGameMgr;
import Server.Model.Hall.dthall;
import activity.ActivityMgr;
import activity.UserActionActivity;
import common.routine.PublishDef;
import rank.RankMgr;
import Server.Model.Base.SecShmUser;
import Server.Model.Base.UserGoldChange;

class SSZTable extends IRoom {

	public static final int US_NULL = 0;
	public static final int US_READY = 1;
	public static final int US_PLAY = 2;

	@Override
	public int GetID() {
		return m_dwTableID;
	};

	@Override
	public int GetCreator() {
		return m_Creator;
	}

	@Override
	public String GetType() {
		return "FJSSZ";
	};

	@Override
	public void OnClientClose(int uid) {
		OnUserStandUp(uid,false);
	};

	public boolean SendData(int wChairID, String sData) {
		// 群发用户
		if (wChairID == Constants.INVALID_CHAIR) {
			for (short i = 0; i < m_pIUserItem.length; i++) {
				if (m_pIUserItem[i].dwUserID > 0) {
					if (m_pIUserItem[i].isAndroidUser == 0) {
						dthall.getInstance().onSend(m_pIUserItem[i].dwUserID, sData);
					} else {
						// mPlayers[i].SendGameMessage(wSubCmdID, pData,
						// wDataSize, data);
					}
				}
			}

			return true;
		} else {
			if (wChairID >= m_pIUserItem.length) {
				dthall.getInstance().onSend(wChairID, sData);
			} else if (m_pIUserItem[wChairID].dwUserID > 0) {
				if (m_pIUserItem[wChairID].isAndroidUser == 0) {
					dthall.getInstance().onSend(m_pIUserItem[wChairID].dwUserID, sData);
				} else {
				}
				return true;
			}
		}

		return false;
	}

	int m_dwLastDoCmdTime;

	public int CanSit(int _Uid) {
		if (m_Creator == _Uid) {
			return 14;
		}
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID == _Uid) {
				SUBCMD_S_OnLine cmd = new SUBCMD_S_OnLine();
				cmd.uid = _Uid;
				cmd.seat = i;
				SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
				return i;
			}
		}
		SHMUser user = dthall.getInstance().getUserInfo(_Uid);
		if (user != null && isZuJuType()) {
			if (m_payWay.equalsIgnoreCase("CLUBPAY") || m_payWay.equalsIgnoreCase("WINPAY")) {
				if(m_payWay.equalsIgnoreCase("CLUBPAY")){
					for (int i = 0; i < 4; i++) {
						if (m_pIUserItem[i].dwUserID == 0) {
							return i;
						}
					}
					return 12;
				}
				if (user.getGem(0) < m_byCostGem * 4) {
					return 11;
				}
			}else{
				if (m_payWay.equalsIgnoreCase("AAPAY") )
				{
					if (user.getGem(0) < m_byCostGem) {
						return 11;
					}
				}
			}
		}

		if(user != null && isGoldType()){
			if(user.getCoin(0) < m_ruchang){
				return 21;
			}
		}
		if(user != null && isDwcType()){
			if(user.getCoin(0) < m_ruchang){
				return 21;
			}
		}
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID == _Uid) {
				SUBCMD_S_OnLine cmd = new SUBCMD_S_OnLine();
				cmd.uid = _Uid;
				cmd.seat = i;
				SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
				return i;
			}
		}
		if (m_locked != 0) {
			return 13;//已经开始 并且房主不让近
		}	
		
		int cntUser = 0;
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntUser++;
			}
		}
		if (cntUser >= m_byMaxUser) {
			return 13;
		}
		
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID == 0) {
				return i;
			}
		}

		return 12;
	};

	Long m_lStartTime;
	long needDealTime;
	int m_dwTableID;
	long m_dismissTimer;

	int m_byAgreeDismiss[] = new int[5];
	int m_teShuPaiList[] = new int[5];

	boolean m_bGameStarted;
	boolean m_bTableStarted;
	int m_byRound;
	int m_byMaxRound;
	int m_byMaxUser;
	int m_wFaPaiDelay; // 发牌延迟
	int m_wRobotSeconds[] = new int[4]; // 等待时长

	public int m_byZhuang;// 当前庄位置

	public int m_byQiangZhuang[] = new int[5]; // 是否抢庄，和抢庄的倍率

	public int m_byCostGem;
	public String m_payWay;
	public String m_fapaiWay;

	public String m_zhuangMode;// 选庄，抢庄，轮庄
	public String m_guize; // 算分规则
	
	boolean isTsp;
	boolean isMaPai;
	boolean isDyise;
	int maPai;
	int jiaFen;
	
	public String m_strParam;
	public String m_strRoomDesc;

	public String[] m_vecParam;

	public int m_vecWinScore[] = new int[4];
	public int m_vecLoseScore[] = new int[4];

	public int m_vecRoundScore[] = new int[4];
	public int m_vecResultScore[] = new int[4];

	public int m_vecIsKaiPaiReady[] = new int[4];
	
	public int m_vecIsTeShuPai[] = new int[4];
	
	public int m_vecDaQiangTime[] = new int[4];
	public int m_vecBeiDaTime[] = new int[4];
	public int m_vecQuanLeiTime[] = new int[4];
	
	public int m_vecRoundDaQiangTime[] = new int[4];
	public int m_vecRoundBeiDaTime[] = new int[4];
	public int m_vecRoundQuanLeiTime[] = new int[4];

	GameLogic m_GameLogic = new GameLogic();

	String m_curStage;

	int m_bTableCardArray[][] = new int[4][13]; // 桌面扑克 //been 4
	int m_cbHandCardData[][] = new int[4][13];
	int m_locked = 0;// 默认允许进

	IServerUserItem m_pIUserItem[] = new IServerUserItem[4]; // 用户指针

	{
		for (int i = 0; i < m_pIUserItem.length; i++) {
			m_pIUserItem[i] = new IServerUserItem();
		}
	}

	int m_LookOnUserID[] = new int[50]; // 观众

	public int m_Creator;// 房主
	public int fangzhu;

	SSZTable() {
		m_bTableStarted = false;
		m_lStartTime = System.currentTimeMillis() / 1000;
		m_bRemove = false;
		m_byCostGem = 0;
		m_payWay = "FZPAY";
		m_fapaiWay = "1ZHANG";

		m_zhuangMode = "WUZHUANG";
		m_guize = "GUIZE_HBL";

		m_byMaxUser = 4;
		
		for(int i = 0;i < 4;i++){
			m_vecDaQiangTime[i] = 0;
			m_vecBeiDaTime[i] = 0;
			m_vecQuanLeiTime[i] = 0;
			
			m_vecRoundDaQiangTime[i] = 0;
			m_vecRoundBeiDaTime[i] = 0;
			m_vecRoundQuanLeiTime[i] = 0;
		}
	}

	void Init(int cbReason) {

		m_dwLastDoCmdTime = 0;
		m_bGameStarted = false;
		m_wFaPaiDelay = 0;

		for (int i = 0; i < 4; i++) {
			m_wRobotSeconds[i] = 0;

			m_byQiangZhuang[i] = 0;
			m_vecIsKaiPaiReady[i] = 0;
			m_vecRoundScore[i] = 0;
			m_vecIsTeShuPai[i] = 0;
			m_teShuPaiList[i] = 0;

			for (int j = 0; j < 13; j++) {
				m_cbHandCardData[i][j] = 0;
			}
		}
		switch (cbReason) {
		case Constants.INIT_TOTAL: {
			m_dismissTimer = 0;
			m_byAgreeDismiss[0] = 0;
			m_byAgreeDismiss[1] = 0;
			m_byAgreeDismiss[2] = 0;
			m_byAgreeDismiss[3] = 0;

			Constants.memset(m_LookOnUserID, 0, Constants.sizeof(m_LookOnUserID)); //

			m_byRound = 0;
			m_byMaxRound = 0;
			m_byZhuang = 0;
			for (int i = 0; i < 4; i++) {
				m_vecWinScore[i] = 0;
				m_vecLoseScore[i] = 0;
				m_vecResultScore[i] = 0;

			}
		}
			break;
		case Constants.INIT_FIRST_ROUND: {

		}
			break;
		case Constants.INIT_NEWROUND: {

		}
			break;
		}
	}

	void KickRobots() {
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].isAndroidUser == 1) {

			}
		}
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].isAndroidUser == 1) {

			}
		}
	}

	boolean HasRobot() {
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].isAndroidUser == 1) {
				return true;
			}
		}
		return false;
	}

	void SendAvatar(int _Uid) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}

		String iter = dthall.getInstance().Handle_OnGetUserAvatar(_Uid);

		SUBCMD_S_Avatar cmd = new SUBCMD_S_Avatar();
		cmd.m_lUid = _Uid;
		if (iter != null) {
			cmd.szAvatarURL = iter;
		} else {
			cmd.szAvatarURL = PublishDef.getDefaultHeadAvatar();
		}

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

	}
	public void SendLocationData(SHMUser user) {
		JSONObject data = new JSONObject();
		data.put("loc", user.getGpsLocInfo());
		data.put("uid", user.getUid());
		data.put("command", "GPS_LOC");
		SendData(Constants.INVALID_CHAIR, data.toString());

		for (short i = 0; i < m_pIUserItem.length; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				if (m_pIUserItem[i].isAndroidUser == 0) {
					int uid = m_pIUserItem[i].dwUserID;
					SHMUser other = dthall.getInstance().getUserInfo(uid);
					if(other != null){
						data.put("uid", other.getUid());
						data.put("loc", other.getGpsLocInfo());
						dthall.getInstance().onSend(user.getUid(), data.toString());
					}
				}
			}
		}
	}

	public void SendGpsData(SHMUser user) {
		JSONObject data = new JSONObject();
		List<Double> gpsData = user.getLocateInfo();
		data.put("lng", gpsData.get(0));
		data.put("lat", gpsData.get(1));
		data.put("command", "GPS_DATA");
		data.put("uid", user.getUid());
		SendData(Constants.INVALID_CHAIR, data.toString());

		for (short i = 0; i < m_pIUserItem.length; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				if (m_pIUserItem[i].isAndroidUser == 0) {
					int uid = m_pIUserItem[i].dwUserID;
					SHMUser other = dthall.getInstance().getUserInfo(uid);
					if (other != null) {
						gpsData = other.getLocateInfo();
						data.put("uid", other.getUid());
						data.put("lng", gpsData.get(0));
						data.put("lat", gpsData.get(1));
						dthall.getInstance().onSend(user.getUid(), data.toString());
					}
				}
			}
		}
	}
	public void OnUserSit(int _Uid) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID < 4) {
			m_pIUserItem[bySeatID].cbOnline = Constants.US_NULL;
		}
		SHMUser user = dthall.getInstance().getUserInfo(_Uid);
		if (user != null) {
			SendGpsData(user);
			SendLocationData(user);
		}
		if (user != null) {
			for (int i = 0; i < 4; i++) {
				if (m_pIUserItem[i].dwUserID == _Uid) {
					m_wRobotSeconds[i] = 0;
					m_pIUserItem[i].dwUserID = _Uid;
					// m_pIUserItem[i].lUserScore = user.getCoin();
					m_pIUserItem[i].wChairID = i;
					m_pIUserItem[i].dwFaceID = 0;
					m_pIUserItem[i].cbGender = user.getSex();
					m_pIUserItem[i].szNickName = user.getNickName(null);
					m_pIUserItem[i].szIP = user.getIP(null);
					m_pIUserItem[i].lUserCoin = user.getCoin(0);

					SUBCMD_S_Sit cmd = new SUBCMD_S_Sit();

					cmd.m_lUid = _Uid;
					cmd.m_wUserChairID = m_pIUserItem[i].wChairID;
					cmd.m_dwTableID = m_dwTableID;
					cmd.m_lUserScore = m_pIUserItem[i].lUserScore;
					cmd.m_cbGender = m_pIUserItem[i].cbGender;
					cmd.m_iAvatar = m_pIUserItem[i].dwFaceID;
					cmd.szNickName = user.getNickName(null);
					cmd.szIP = user.getIP(null);
					cmd.m_lUserCoin = user.getCoin(0);

					SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

					if (m_bGameStarted) {
						SendGameScene(_Uid, Constants.GS_PLAYING);
					} else {
						SendGameScene(_Uid, Constants.GS_FREE);
					}

					SendAvatar(m_pIUserItem[i].dwUserID);

					return;
				}
			}
			for (int i = 0; i < 4; i++) {
				if (m_pIUserItem[i].dwUserID == 0) {
					m_wRobotSeconds[i] = 0;
					m_pIUserItem[i].dwUserID = _Uid;
					m_pIUserItem[i].lUserScore = 0;
					m_pIUserItem[i].wChairID = i;
					m_pIUserItem[i].dwFaceID = 0;
					m_pIUserItem[i].cbGender = user.getSex();
					m_pIUserItem[i].szNickName = user.getNickName(null);
					m_pIUserItem[i].szIP = user.getIP(null);
					m_pIUserItem[i].lUserCoin = user.getCoin(0);

					SUBCMD_S_Sit cmd = new SUBCMD_S_Sit();

					cmd.m_lUid = _Uid;
					cmd.m_wUserChairID = m_pIUserItem[i].wChairID;
					cmd.m_dwTableID = m_dwTableID;
					cmd.m_lUserScore = m_pIUserItem[i].lUserScore;
					cmd.m_cbGender = m_pIUserItem[i].cbGender;
					cmd.m_iAvatar = m_pIUserItem[i].dwFaceID;
					cmd.szNickName = user.getNickName(null);
					cmd.szIP = user.getIP(null);
					cmd.m_lUserCoin = user.getCoin(0);

					SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

					if (m_bGameStarted) {
						SendGameScene(_Uid, Constants.GS_PLAYING);
					} else {
						SendGameScene(_Uid, Constants.GS_FREE);
					}

					return;
				}
			}
		}

		return;
	}

	int FindSeat(long _Uid) {
		for (int bySeatID = 0; bySeatID < 4; bySeatID++) {
			if (m_pIUserItem[bySeatID].dwUserID == _Uid) {
				return bySeatID;
			}
		}
		return Constants.INVALID_CHAIR;
	}

	int GetPlayerCount() {
		int cntPlayer = 0;
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntPlayer++;
			}
		}
		return cntPlayer;
	}

	int GetRobotCount() {
		int cntPlayer = 0;
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].isAndroidUser == 1) {
				cntPlayer++;
			}
		}
		return cntPlayer;
	}

	public void OnUserStandUp(int _Uid,boolean leaveroom) {
		int wChairID = FindSeat(_Uid);
		
		if(m_bTableStarted == false && wChairID != 0 && leaveroom == true){
			SUBCMD_S_StandUp cmd = new SUBCMD_S_StandUp();
			cmd.m_lUid = _Uid;
			cmd.m_wUserChairID = wChairID;
			cmd.m_dwTableID = m_dwTableID;
			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
		}


		if (m_bTableStarted == false && leaveroom == true) {
			if (wChairID > 0 && wChairID < 4) {
				m_pIUserItem[wChairID].Init();
				QpGameMgr.getInstance().OnRemoveUserRoom(_Uid);
			} else {
			}
		} else {
			if (wChairID < 4) {
				m_pIUserItem[wChairID].cbOnline = Constants.US_OFFLINE;
			}
		}
	}

	public void SetParam(String szParam) {
		m_strParam = szParam;

		//m_vecParam = m_strParam.split(";");
	}

	boolean SendGameScene(int _Uid, int bGameStatus) {
		if (m_strParam.length() > 0) {
			SUBCMD_S_Param cmd = new SUBCMD_S_Param();
			cmd.m_dwTableID = m_dwTableID;
			cmd.szParam = m_strParam;
			cmd.roomDesc = m_strRoomDesc;
			SendData(_Uid, cmd.getJsonData());
		}
		int cntUser = 0;
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntUser++;
			}
		}
		{
			SUBCMD_S_Table cmd = new SUBCMD_S_Table();
			cmd.m_dwTableID = m_dwTableID;// 房间ID
			cmd.m_bGameStarted = m_bGameStarted ? 1 : 0;// 是否开始
			cmd.m_bTableStarted = m_bTableStarted ? 1 : 0;// 是否开始
			cmd.m_byRound = m_byRound;
			cmd.m_byMaxRound = m_byMaxRound;
			cmd.m_cntUser = cntUser;// 玩家
			cmd.m_cntLookon = 1;// 观众
			Constants.memset(cmd.m_seatInfo, 0, Constants.sizeof(cmd.m_seatInfo));

			for (int i = 0; i < 4; i++) {
				cmd.m_seatInfo[i].m_lUid = m_pIUserItem[i].dwUserID;// UID
				cmd.m_seatInfo[i].m_iTotalCoin = m_pIUserItem[i].lUserScore;// COIN
				cmd.m_seatInfo[i].m_NickName = m_pIUserItem[i].szNickName;// NICKNAME
				cmd.m_seatInfo[i].m_iBetCoin = 0;//
				cmd.m_seatInfo[i].m_cbGender = m_pIUserItem[i].cbGender;
				cmd.m_seatInfo[i].m_iAvatar = m_pIUserItem[i].dwFaceID;//
				cmd.m_seatInfo[i].m_cbUserStatus = m_pIUserItem[i].cbUserStatus;// 用户状态
				cmd.m_seatInfo[i].m_iVipLevel = m_pIUserItem[i].cbVipLevel;// VIP
																			// LEVEL
				cmd.m_seatInfo[i].m_iTitle = 0;// 称号
				cmd.m_seatInfo[i].m_isLookon = 0;// 是否观众
				cmd.m_seatInfo[i].szIP = m_pIUserItem[i].szIP;// NICKNAME
				cmd.m_seatInfo[i].m_iTotalCoin = m_pIUserItem[i].lUserCoin;
			}

			SendData(_Uid, cmd.getJsonData());
			for (int i = 0; i < 4; i++) {
				if (m_pIUserItem[i].dwUserID > 0) {
					SendAvatar(m_pIUserItem[i].dwUserID);
				}
			}
		}

		if (m_bTableStarted == true) {
			{
				SUBCMD_S_STAGE cmd = new SUBCMD_S_STAGE();

				cmd.szStage = m_curStage;
				cmd.needDealTime = (int) needDealTime;
				for (int i = 0; i < 4; i++) {
					for (int k = 0; k < 13; k++) {
						cmd.m_bTableCardArray[i][k] = m_bTableCardArray[i][k]; // 桌面扑克
					}
				}

				SendData(_Uid, cmd.getJsonData());
			}

			if (m_bGameStarted == true) {
				{
					SUBCMD_S_TableBegin cmd = new SUBCMD_S_TableBegin();

					cmd.byRound = m_byRound;
					cmd.byMaxRound = m_byMaxRound;

					for (int i = 0; i < 4; i++) {
						if (m_pIUserItem[i].dwUserID > 0) {
							cmd.bIsReady[i] = m_pIUserItem[i].cbUserStatus;
							cmd.bIsKaiPaiReady[i] = m_vecIsKaiPaiReady[i];
						}
					}
					SendData(_Uid, cmd.getJsonData());
				}
				{

					for (int i = 0; i < 4; i++) {
						if (m_pIUserItem[i].dwUserID > 0) {
							SUBCMD_S_TableSide cmd = new SUBCMD_S_TableSide();
							cmd.bySeatID = i;
							cmd.bIsReady = m_pIUserItem[i].cbUserStatus;
							cmd.bIsKaiPaiReady = m_vecIsKaiPaiReady[i];
							for (int k = 0; k < 13; k++) {
								cmd.byPai[k] = m_bTableCardArray[i][k];
							}
							if(isMaPai == true){
								cmd.maPai = maPai;
							}
							cmd.teshuPai = isTeShuPai(i);
							SendData(_Uid, cmd.getJsonData());
						}
					}
				}
				{
					SUBCMD_S_TableEnd cmd = new SUBCMD_S_TableEnd();

					SendData(_Uid, cmd.getJsonData());
				}
			}
		}

		switch (bGameStatus) {
		case Constants.GS_FREE: // 空闲状态
		{
			// 构造数据

			return true;
		}
		case Constants.GS_PLAYING: // 游戏状态
		{
			// 构造数据

			return true;
		}
		}

		return true;
	}

	void FaPai() {
		m_bTableStarted = true;
		m_bGameStarted = true;

		m_curStage = "STAGE_FAPAI";

		needDealTime = 8;

		if (m_byRound == 0) {
			m_byRound = 1;
		}
		for(int i = 0;i < 4;i++){
			m_vecDaQiangTime[i] += m_vecRoundDaQiangTime[i];
			m_vecBeiDaTime[i] += m_vecRoundBeiDaTime[i];
			m_vecQuanLeiTime[i] += m_vecRoundQuanLeiTime[i];
		}
		for (int i = 0; i < 4; i++) {
			m_vecRoundDaQiangTime[i] = 0;
			m_vecRoundBeiDaTime[i] = 0;
			m_vecRoundQuanLeiTime[i] = 0;
		}
		
		int cbTableCardArray[] = new int[52];
		if(isDyise == true){
			m_GameLogic.RandCardList5(cbTableCardArray, 52); // 计算数组的个数
		}else{
			m_GameLogic.RandCardList(cbTableCardArray, 52); // 计算数组的个数
		}
		
		int cbTableCardValueArray[][] = new int[4][13];


		for (int i = 0; i < 52; i++) {
			int x = i / 13;
			int y = i % 13;
			m_bTableCardArray[x][y] = cbTableCardArray[i];
			cbTableCardValueArray[x][y] = m_GameLogic.GetCardValue(m_bTableCardArray[x][y]);
		}
		//test
//		 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, // 方块
//			// A
//			// -
//			// K
//0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, // 梅花
//// A
//// -
//// K
//0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, // 红桃
//// A
//// -
//// K
//0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, // 黑桃
//// A
//// -
//// K
//		int test[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D};
//		int test2[] = {0x01, 0x02, 0x03, 0x11, 0x12, 0x13, 0x14, 0x15, 0x21, 0x22, 0x23, 0x24, 0x25};
////		int test3[] = {0x01, 0x02, 0x03, 0x11, 0x12, 0x13, 0x14, 0x15, 0x21, 0x22, 0x23, 0x24, 0x25};
//		int test4[] = { 0x11, 0x2D, 0x0D, 0x2C, 0x2B, 0x2A, 0x19, 0x29, 0x36, 0x15, 0x25, 0x25, 0x33};
////		
//		for(int i=0;i<13;i++){
//			
//			m_bTableCardArray[0][i] = test4[i];
//			
//		}
//
//		for(int i=0;i<13;i++){
//			
//			m_bTableCardArray[1][i] = test2[i];
//			
//		}
		
		
//		int test1[] = {49,13,44,42,26,56,24,7,37,20,35,19,3};
//		int test2[] = {33,17,1,61,29,11,10,57,25,8,6,5,50};
//		int test3[] = {45,59,27,41,9,40,23,54,38,53,36,4,18};
//		int test4[] = {60,28,12,43,58,55,39,22,21,52,51,34,2};
		
//		int test1[] = {12,59,11,24,23,54,6,5,52,20,51,35,34};
//		int test2[] = {49,17,1,45,13,44,58,42,26,25,9,56,40};
//		int test3[] = {33,61,29,60,28,43,27,10,8,22,4,50,18};
//		int test4[] = {57,41,55,39,7,38,53,37,21,36,19,3,2};
//	
//		for(int i=0;i<13;i++){
//			
//			m_bTableCardArray[0][i] = test1[i];
//			
//		}
//	    for(int i=0;i<13;i++){
//			
//			m_bTableCardArray[1][i] = test2[i];
//			
//		}
//        for(int i=0;i<13;i++){
//			
//			m_bTableCardArray[2][i] = test3[i];
//			
//		}
//	    for(int i=0;i<13;i++){
//			
//			m_bTableCardArray[3][i] = test4[i];
//			
//		}
	   
		
		SUBCMD_S_FaPai cmd = new SUBCMD_S_FaPai();
		cmd.byRound = m_byRound;
		cmd.byMaxRound = m_byMaxRound;
		for (int i = 0; i < 4; i++) {
			m_vecRoundScore[i] = 0;
			if (m_pIUserItem[i].dwUserID > 0 && (m_pIUserItem[i].cbUserStatus == Constants.US_READY
					|| m_pIUserItem[i].cbUserStatus == Constants.US_PLAY)) {
				m_pIUserItem[i].cbUserStatus = Constants.US_PLAY;
				cmd.vecIsReady[i] = 1;
			} else {
				m_pIUserItem[i].cbUserStatus = Constants.US_NULL;
				cmd.vecIsReady[i] = 0;
			}
		}

		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].cbUserStatus == Constants.US_PLAY) {
				for (int k = 0; k < 13; k++) {
					cmd.byPai[k] = m_bTableCardArray[i][k];
				}
				if(isMaPai == true){
					cmd.maPai = maPai;
				}
				cmd.teshuPai = isTeShuPai(i);
				SendData(m_pIUserItem[i].dwUserID, cmd.getJsonData());
			}
		}
		
	
	}
int isTeShuPai(int seat){
		
		//TODO
		if(m_GameLogic.getHandCardType(m_bTableCardArray[seat], 13) == Constants.CT_ZHIZUNQINGLONG){//至尊青龙
			for(int i=0;i<13;i++){
				m_cbHandCardData[seat][i] = m_bTableCardArray[seat][i];
			}
			
			return Constants.CT_ZHIZUNQINGLONG;
			
		}else if(m_GameLogic.getHandCardType(m_bTableCardArray[seat], 13) == Constants.CT_YITIAOLONG){//一条龙
			for(int i=0;i<13;i++){
				m_cbHandCardData[seat][i] = m_bTableCardArray[seat][i];
			}
           
			return Constants.CT_YITIAOLONG;
		}else if(m_GameLogic.getHandCardType(m_bTableCardArray[seat], 13) == Constants.CT_SITAOSANTIAO){//四条三套
			for(int i=0;i<13;i++){
				m_cbHandCardData[seat][i] = m_bTableCardArray[seat][i];
			}
			return Constants.CT_SITAOSANTIAO;
			 
		}else if(m_GameLogic.getHandCardType(m_bTableCardArray[seat], 13) == Constants.CT_LIUDUIBAN){//六对半
			for(int i=0;i<13;i++){
				m_cbHandCardData[seat][i] = m_bTableCardArray[seat][i];
			}
			return Constants.CT_LIUDUIBAN;
		}
		
		int[] temCardArray = new int[13];
		for(int i=0;i<13;i++){
			
			temCardArray[i] = m_GameLogic.GetCardLogicValue(m_bTableCardArray[seat][i]);
		}
		
		m_GameLogic.SortCardListBeginSmall(temCardArray,13);
		
		if(SanShunZi(temCardArray)){
			for(int i=0;i<13;i++){
				m_cbHandCardData[seat][i] = m_bTableCardArray[seat][i];
			}
			return Constants.CT_SANSHUNZI;
		}
		if(SanShunZi2(temCardArray)){
			for(int i=0;i<13;i++){
				m_cbHandCardData[seat][i] = m_bTableCardArray[seat][i];
			}
			return Constants.CT_SANSHUNZI;
		}
		if(SanShunZi3(temCardArray)){
			for(int i=0;i<13;i++){
				m_cbHandCardData[seat][i] = m_bTableCardArray[seat][i];
			}
			return Constants.CT_SANSHUNZI;
		}
		
        for(int i=0;i<13;i++){
			
			temCardArray[i] = m_GameLogic.GetCardValue(m_bTableCardArray[seat][i]);
		}
    	m_GameLogic.SortCardListBeginSmall(temCardArray,13);
        if(SanShunZi(temCardArray)){
			for(int i=0;i<13;i++){
				m_cbHandCardData[seat][i] = m_bTableCardArray[seat][i];
			}
			return Constants.CT_SANSHUNZI;
		}
    	if(SanShunZi2(temCardArray)){
			for(int i=0;i<13;i++){
				m_cbHandCardData[seat][i] = m_bTableCardArray[seat][i];
			}
			return Constants.CT_SANSHUNZI;
		}
		if(SanShunZi3(temCardArray)){
			for(int i=0;i<13;i++){
				m_cbHandCardData[seat][i] = m_bTableCardArray[seat][i];
			}
			return Constants.CT_SANSHUNZI;
		}
		
		int hei=0;int hong=0; int mei = 0; int fang = 0;
		for(int i=0;i<13;i++){
			int cardIdx = m_bTableCardArray[seat][i];
			
			if(cardIdx/16 == 0){
				fang ++;
			}
            if(cardIdx/16 == 1){
				mei ++;
			}
			if(cardIdx/16 == 2){
				hong++;
			}
            if(cardIdx/16 == 3){
				hei++;
			}
		
		}
		
		if((fang ==3||mei ==3||hong ==3||hei ==3)&&(fang ==5||mei ==5||hong ==5||hei ==5)){
			
			if(fang == 5){
				if(mei ==5||hong ==5||hei ==5){
					for(int i=0;i<13;i++){
						m_cbHandCardData[seat][i] = m_bTableCardArray[seat][i];
					}
					return Constants.CT_SANTONGHUA;
				}
			}
			if(mei == 5){
				if(fang ==5||hong ==5||hei ==5){
					for(int i=0;i<13;i++){
						m_cbHandCardData[seat][i] = m_bTableCardArray[seat][i];
					}
					return Constants.CT_SANTONGHUA;
				}
			}
			if(hong == 5){
				if(fang ==5||mei ==5||hei ==5){
					for(int i=0;i<13;i++){
						m_cbHandCardData[seat][i] = m_bTableCardArray[seat][i];
					}
					return Constants.CT_SANTONGHUA;
				}
			}
			if(hei == 5){
				if(fang ==5||mei ==5||hong ==5){
					for(int i=0;i<13;i++){
						m_cbHandCardData[seat][i] = m_bTableCardArray[seat][i];
					}
					return Constants.CT_SANTONGHUA;
				}
			}
		
		}
		
         if((fang ==8||mei ==8||hong ==8||hei ==8)&&(fang ==5||mei ==5||hong ==5||hei ==5)){
			
			if(fang == 8){
				if(mei ==5||hong ==5||hei ==5){
					for(int i=0;i<13;i++){
						m_cbHandCardData[seat][i] = m_bTableCardArray[seat][i];
					}
					return Constants.CT_SANTONGHUA;
				}
			}
			if(mei == 8){
				if(fang ==5||hong ==5||hei ==5){
					for(int i=0;i<13;i++){
						m_cbHandCardData[seat][i] = m_bTableCardArray[seat][i];
					}
					return Constants.CT_SANTONGHUA;
				}
			}
			if(hong == 8){
				if(fang ==5||mei ==5||hei ==5){
					for(int i=0;i<13;i++){
						m_cbHandCardData[seat][i] = m_bTableCardArray[seat][i];
					}
					return Constants.CT_SANTONGHUA;
				}
			}
			if(hei == 8){
				if(fang ==5||mei ==5||hong ==5){
					for(int i=0;i<13;i++){
						m_cbHandCardData[seat][i] = m_bTableCardArray[seat][i];
					}
					return Constants.CT_SANTONGHUA;
				}
			}
		
		}
         
         if((fang ==3||mei ==3||hong ==3||hei ==8)&&(fang ==10||mei ==10||hong ==10||hei ==10)){
 			
 			if(fang == 3){
 				if(mei ==10||hong ==10||hei ==10){
 					for(int i=0;i<13;i++){
 						m_cbHandCardData[seat][i] = m_bTableCardArray[seat][i];
 					}
 					return Constants.CT_SANTONGHUA;
 				}
 			}
 			if(mei == 3){
 				if(fang ==10||hong ==10||hei ==10){
 					for(int i=0;i<13;i++){
 						m_cbHandCardData[seat][i] = m_bTableCardArray[seat][i];
 					}
 					return Constants.CT_SANTONGHUA;
 				}
 			}
 			if(hong == 3){
 				if(fang ==10||mei ==10||hei ==10){
 					for(int i=0;i<13;i++){
 						m_cbHandCardData[seat][i] = m_bTableCardArray[seat][i];
 					}
 					return Constants.CT_SANTONGHUA;
 				}
 			}
 			if(hei == 3){
 				if(fang ==10||mei ==10||hong ==10){
 					for(int i=0;i<13;i++){
 						m_cbHandCardData[seat][i] = m_bTableCardArray[seat][i];
 					}
 					return Constants.CT_SANTONGHUA;
 				}
 			}
 		
 		} 
		
		
		return 0;
	}
	
	public static boolean GetShunZiCount(List<Integer> allPokers, int index, int count)
    {
        int pipei = 0;
        int[] pokers = new int[count];
        pokers[pipei++] = allPokers.get(index);
        allPokers.remove(index);
        for (int i = 0; i < allPokers.size() && pipei < count; i++)
        {
            if (allPokers.get(i).intValue() == pokers[pipei - 1] + 1)
            {
                pokers[pipei++] = allPokers.get(i);
                allPokers.remove(i);
                i--;
            }
        }
        if (pipei == count)
        {
        	System.out.println(pokers);
            return true;
        }
        return false;
    }

    public static boolean SanShunZi(int[] pokers)
    {
        //pokers先按照顺子值大小排序 //  或者GetCardLogicValue ST_ORDER排序 有王和2 直接返回false
        //A  2    3 ~ 10,11,12,13  王
        //14 100  3 ~ 10,11,12,13  100
        List<Integer> allPokers = new ArrayList<Integer>(pokers.length);
        for (int i = 0; i < pokers.length - 4; i++)
        {
            allPokers.clear();
            for (int j = 0; j < pokers.length; j++)
            {
                allPokers.add(pokers[j]);
            }
            if (GetShunZiCount(allPokers, i, 3))
            {//先找三个的

                if (GetShunZiCount(allPokers, 0, 5))////因为是先找的三个 的 所以必须是第一个就开始
                {
                    if (GetShunZiCount(allPokers, 0, 5))
                    {
                        return true;
                        
                    }
                }
            }
        }
        return false;
    }

    public static boolean SanShunZi2(int[] pokers)
    {
        //pokers先按照顺子值大小排序 //  或者GetCardLogicValue ST_ORDER排序 有王和2 直接返回false
        //A  2    3 ~ 10,11,12,13  王
        //14 100  3 ~ 10,11,12,13  100
        List<Integer> allPokers = new ArrayList<Integer>(pokers.length);
        for (int i = 0; i < pokers.length - 4; i++)
        {
            allPokers.clear();
            for (int j = 0; j < pokers.length; j++)
            {
                allPokers.add(pokers[j]);
            }
            if (GetShunZiCount(allPokers, i, 5))
            {//先找三个的

                if (GetShunZiCount(allPokers, 0, 5))////因为是先找的三个 的 所以必须是第一个就开始
                {
                    if (GetShunZiCount(allPokers, 0, 3))
                    {
                        return true;
                        
                    }
                }
            }
            
        }
        return false;
    }
    
    public static boolean SanShunZi3(int[] pokers)
    {
        //pokers先按照顺子值大小排序 //  或者GetCardLogicValue ST_ORDER排序 有王和2 直接返回false
        //A  2    3 ~ 10,11,12,13  王
        //14 100  3 ~ 10,11,12,13  100
        List<Integer> allPokers = new ArrayList<Integer>(pokers.length);
        for (int i = 0; i < pokers.length - 4; i++)
        {
            allPokers.clear();
            for (int j = 0; j < pokers.length; j++)
            {
                allPokers.add(pokers[j]);
            }
            if (GetShunZiCount(allPokers, i, 5))
            {//先找三个的

                if (GetShunZiCount(allPokers, 0, 3))////因为是先找的三个 的 所以必须是第一个就开始
                {
                    if (GetShunZiCount(allPokers, 0, 5))
                    {
                        return true;
                        
                    }
                }
            }
            
        }
        return false;
    }
	public static int GetNextUser(int bySeatID) {
		return (bySeatID + 1) % 4;
	}
	public int getBeiShuWithType(int teshuPaiType){
		int beishu = 4;
		if(teshuPaiType == 11){
			beishu = 108;
		}
		if(teshuPaiType == 12){
			beishu = 26;
		}
		if(teshuPaiType == 13){
			beishu = 8;
		}
		if(teshuPaiType == 14){
			beishu = 4;
		}
		if(teshuPaiType == 15){
			beishu = 4;
		}
		if(teshuPaiType == 16){
			beishu = 4;
		}
		
		return beishu;
		
	}

	void KaiPai_HBL() {
		int cbCardType[][] = new int[4][3];
		int vecReadyCard[][][] = new int[4][3][5];
		for (int i = 0; i < 4; i++) {
			vecReadyCard[i][0][0] = m_cbHandCardData[i][0];
			vecReadyCard[i][0][1] = m_cbHandCardData[i][1];
			vecReadyCard[i][0][2] = m_cbHandCardData[i][2];
			m_GameLogic.SortCardList(vecReadyCard[i][0], 3);
			cbCardType[i][0] = m_GameLogic.GetCardType(vecReadyCard[i][0], 3);

			vecReadyCard[i][1][0] = m_cbHandCardData[i][3];
			vecReadyCard[i][1][1] = m_cbHandCardData[i][4];
			vecReadyCard[i][1][2] = m_cbHandCardData[i][5];
			vecReadyCard[i][1][3] = m_cbHandCardData[i][6];
			vecReadyCard[i][1][4] = m_cbHandCardData[i][7];
			m_GameLogic.SortCardList(vecReadyCard[i][1], 5);
			cbCardType[i][1] = m_GameLogic.GetCardType(vecReadyCard[i][1], 5);

			vecReadyCard[i][2][0] = m_cbHandCardData[i][8];
			vecReadyCard[i][2][1] = m_cbHandCardData[i][9];
			vecReadyCard[i][2][2] = m_cbHandCardData[i][10];
			vecReadyCard[i][2][3] = m_cbHandCardData[i][11];
			vecReadyCard[i][2][4] = m_cbHandCardData[i][12];
			m_GameLogic.SortCardList(vecReadyCard[i][2], 5);
			cbCardType[i][2] = m_GameLogic.GetCardType(vecReadyCard[i][2], 5);
		}

		int roundTmpScore[] = new int[4];
		int roundSuanFen[][][] = new int[4][4][3];

		int teshupai = 0;
		for (int i = 0; i < 4; i++) {
			
			if(m_teShuPaiList[i]>0){
				
				teshupai ++;
			}
			
		}
		
		
		int[] maPaiList = new int[4];
		for (int i = 0; i < 4; i++) {
			if(isMaPai&&m_GameLogic.getMaPai(m_bTableCardArray[i], 13,maPai) == 1){
				
				maPaiList[i] = 1;
			}
			
		}
		
		if (m_zhuangMode.equalsIgnoreCase("WUZHUANG")) {
			
		
			for (int i = 0; i < 4; i++) {
				int byNext = i;
				if (vecReadyCard[i][0][0] == 0) {
					continue;
				}
				
					for (int j = 0; j < 3; j++) {
						byNext = GetNextUser(byNext);
						if (vecReadyCard[byNext][0][0] == 0) {
							continue;
						}
						int curScore = 0;
						int prizeScore = 0;
						int cntWin = 0;
						int cntLose = 0;
					
						
						if((m_teShuPaiList[i]>0&&m_teShuPaiList[byNext] == 0)||(m_teShuPaiList[i]>m_teShuPaiList[byNext])){
							
						   curScore = getBeiShuWithType(m_teShuPaiList[i]);
					   }else if((m_teShuPaiList[i] == 0&&m_teShuPaiList[byNext] > 0)||(m_teShuPaiList[i]<m_teShuPaiList[byNext])){
						   curScore = -getBeiShuWithType(m_teShuPaiList[byNext]);
					   }else{
						   

							for (int k = 0; k < 3; k++) {
								int suanfen = 0;
								
								int ret = m_GameLogic.CompareCard(vecReadyCard[i][k], vecReadyCard[byNext][k], 5);

								if (k == 0) {
									
									if (ret == 2) {
										cntWin++;
										if (cbCardType[i][0] == Constants.CT_THREE_TIAO) {
											curScore += 3;
											prizeScore += 2;
											suanfen = 3;
										} else {
											curScore += 1;
											suanfen = 1;
										}
									}
									if (ret == 1) {
										cntLose++;
										if (cbCardType[byNext][0] == Constants.CT_THREE_TIAO) {
											curScore += -3;
											prizeScore += -2;
											suanfen = -3;
										} else {
											curScore += -1;
											suanfen = -1;
										}
									}
									if (ret == 0) {
										curScore = 0;
									}
									
								
								}

								if (k == 1) {
									
									if (ret == 2) {
										cntWin++;
										if (cbCardType[i][1] == Constants.CT_HU_LU) {
											curScore += 2;
											prizeScore += 1;
											suanfen = 2;
										} else if (cbCardType[i][1] == Constants.CT_TIE_ZHI) {
											curScore += 8;
											prizeScore += 7;
											suanfen = 8;
										} else if (cbCardType[i][1] == Constants.CT_TONG_HUA_SHUN
												|| cbCardType[i][1] == Constants.CT_KING_TONG_HUA_SHUN) {
											curScore += 10;
											prizeScore += 9;
											suanfen = 10;
										}else if (cbCardType[i][1] == Constants.CT_WU_TONG) {
											curScore += 20;
											prizeScore += 19;
											suanfen = 20;
										} else {
											curScore += 1;
											suanfen = 1;
										}
									}
									if (ret == 1) {
										cntLose++;
										if (cbCardType[byNext][1] == Constants.CT_HU_LU) {
											curScore += -2;
											prizeScore += -1;
											suanfen = -2;
										} else if (cbCardType[byNext][1] == Constants.CT_TIE_ZHI) {
											curScore += -8;
											prizeScore += -7;
											suanfen = -8;
										} else if (cbCardType[byNext][1] == Constants.CT_TONG_HUA_SHUN
												|| cbCardType[byNext][1] == Constants.CT_KING_TONG_HUA_SHUN) {
											curScore += -10;
											prizeScore += -9;
											suanfen = -10;
										} else if (cbCardType[byNext][1] == Constants.CT_WU_TONG) {
											curScore += -20;
											prizeScore += -19;
											suanfen = -20;
										}else {
											curScore += -1;
											suanfen = -1;
										}
									}
									if (ret == 0) {
										curScore = 0;
									}
								
								}

								if (k == 2) {
									
									if (ret == 2) {
										cntWin++;
										if (cbCardType[i][2] == Constants.CT_TIE_ZHI) {
											curScore += 4;
											prizeScore += 3;
											suanfen = 4;
										} else if (cbCardType[i][2] == Constants.CT_TONG_HUA_SHUN
												|| cbCardType[i][2] == Constants.CT_KING_TONG_HUA_SHUN) {
											curScore += 5;
											prizeScore += 4;
											suanfen = 5;
										} else if (cbCardType[i][2] == Constants.CT_WU_TONG) {
											curScore += 10;
											prizeScore += 9;
											suanfen = 10;
										}else {
											curScore += 1;
											suanfen = 1;
										}
									}
									if (ret == 1) {
										cntLose++;
										if (cbCardType[byNext][2] == Constants.CT_TIE_ZHI) {
											curScore += -4;
											prizeScore += -3;
											suanfen = -4;
										} else if (cbCardType[byNext][2] == Constants.CT_TONG_HUA_SHUN
												|| cbCardType[byNext][2] == Constants.CT_KING_TONG_HUA_SHUN) {
											curScore += -5;
											prizeScore += -4;
											suanfen = -5;
										}else if (cbCardType[byNext][2] == Constants.CT_WU_TONG) {
											curScore += -10;
											prizeScore += -9;
											suanfen = -10;
										} else {
											curScore += -1;
											suanfen = -1;
										}
									}
									if (ret == 0) {
										curScore = 0;
									}
									
									
								}

								roundSuanFen[i][byNext][k] = suanfen;

							}
					   }
					
					
						int t = 1;
						if( maPaiList[i] == 1 || maPaiList[byNext] ==  1){
							t = 2;
						}
						if (cntWin == 3) // 通吃
						{
							
							if(jiaFen == 1){
								roundTmpScore[i] += (3 +1+ prizeScore) * t;
							}else if(jiaFen == 2){
								roundTmpScore[i] += (3 + prizeScore)*2 *t;
							}
						} else if (cntLose == 3) // 通吃
						{
							
							if(jiaFen == 1){
								roundTmpScore[i] += (-3-1 + prizeScore)*t;
							}else if(jiaFen == 2){
								roundTmpScore[i] += (-3 + prizeScore)*2*t;
							}
						} else {
							roundTmpScore[i] += curScore * t;
						}
						
						
					}
				
				
			}
			
		
			
			
			
			if(teshupai == 0){
				for (int i = 0; i < 4; i++) {
					int cntHBLWin = 0;
					int cntHBLLose = 0;
					int byNext = i;
					for (int j = 0; j < 3; j++) {
						byNext = GetNextUser(byNext);
						if (roundSuanFen[i][byNext][0] > 0 && roundSuanFen[i][byNext][1] > 0
								&& roundSuanFen[i][byNext][2] > 0) {
							cntHBLWin++;
						}
						if (cntHBLWin == 3) {
							roundTmpScore[i] = roundTmpScore[i]*2;
							int byNext1 = GetNextUser(i);
							int byNext2 = GetNextUser(byNext1);
							int byNext3 = GetNextUser(byNext2);
							
							if (roundSuanFen[i][byNext1][0] > 0 && roundSuanFen[i][byNext1][1] > 0
									&& roundSuanFen[i][byNext1][2] > 0) {
								
								int maPai = 1;
								if( maPaiList[i] == 1 || maPaiList[byNext1] ==  1){
									maPai = 2;
								}
								int tmpScore = 0;
								if(jiaFen == 1){
									 tmpScore = roundTmpScore[byNext1] +(roundSuanFen[i][byNext1][0]+roundSuanFen[i][byNext1][1]+roundSuanFen[i][byNext1][2]+1)*maPai;
									 roundTmpScore[byNext1] = -(roundSuanFen[i][byNext1][0]+roundSuanFen[i][byNext1][1]+roundSuanFen[i][byNext1][2]+1)*2*maPai+tmpScore;

								}
								if(jiaFen == 2){
									 tmpScore = roundTmpScore[byNext1] +(roundSuanFen[i][byNext1][0]+roundSuanFen[i][byNext1][1]+roundSuanFen[i][byNext1][2])*2*maPai;
									 roundTmpScore[byNext1] = -(roundSuanFen[i][byNext1][0]+roundSuanFen[i][byNext1][1]+roundSuanFen[i][byNext1][2])*4*maPai+tmpScore;

								}
							}
							if (roundSuanFen[i][byNext2][0] > 0 && roundSuanFen[i][byNext2][1] > 0
									&& roundSuanFen[i][byNext2][2] > 0) {
								int maPai = 1;
								if( maPaiList[i] == 1 || maPaiList[byNext2] ==  1){
									maPai = 2;
								}
								int tmpScore = 0;
								if(jiaFen == 1){
									 tmpScore = roundTmpScore[byNext2] + (roundSuanFen[i][byNext2][0]+roundSuanFen[i][byNext2][1]+roundSuanFen[i][byNext2][2]+1)*maPai;
									roundTmpScore[byNext2] = -(roundSuanFen[i][byNext2][0]+roundSuanFen[i][byNext2][1]+roundSuanFen[i][byNext2][2]+1)*2*maPai+tmpScore;

								}
	                            if(jiaFen == 2){
									 tmpScore = roundTmpScore[byNext2] + (roundSuanFen[i][byNext2][0]+roundSuanFen[i][byNext2][1]+roundSuanFen[i][byNext2][2])*2*maPai;
									roundTmpScore[byNext2] = -(roundSuanFen[i][byNext2][0]+roundSuanFen[i][byNext2][1]+roundSuanFen[i][byNext2][2])*4*maPai+tmpScore;

								}
							}
							if (roundSuanFen[i][byNext3][0] > 0 && roundSuanFen[i][byNext3][1] > 0
									&& roundSuanFen[i][byNext3][2] > 0) {
								int maPai = 1;
								if( maPaiList[i] == 1 || maPaiList[byNext3] ==  1){
									maPai = 2;
								}
								int tmpScore = 0;
								if(jiaFen == 1){
									 tmpScore = roundTmpScore[byNext3] + (roundSuanFen[i][byNext3][0]+roundSuanFen[i][byNext3][1]+roundSuanFen[i][byNext3][2]+1)*maPai;
									 roundTmpScore[byNext3] = -(roundSuanFen[i][byNext3][0]+roundSuanFen[i][byNext3][1]+roundSuanFen[i][byNext3][2]+1)*2*maPai+tmpScore;

								}
	                            if(jiaFen == 2){
									 tmpScore = roundTmpScore[byNext3] + (roundSuanFen[i][byNext3][0]+roundSuanFen[i][byNext3][1]+roundSuanFen[i][byNext3][2])*2*maPai;
									 roundTmpScore[byNext3] = -(roundSuanFen[i][byNext3][0]+roundSuanFen[i][byNext3][1]+roundSuanFen[i][byNext3][2])*4*maPai+tmpScore;

								}
								
							}
							
							
						}
					}
				}
			}
		

		
			
		} 
	


		for (int i = 0; i < 4; i++) {
			m_vecRoundScore[i] = roundTmpScore[i];
		}
		SUBCMD_S_SuanFen cmd = new SUBCMD_S_SuanFen();

		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				for (int k = 0; k < 3; k++) {
					cmd.nScore[i][j][k] = roundSuanFen[i][j][k];
				}
			}
		}

		String sData = cmd.getJsonData();
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				SendData(m_pIUserItem[i].dwUserID, sData);
			}
		}

		OnEventGameEnd(Constants.INVALID_CHAIR, Constants.GER_NORMAL);
	}


	void KaiPai_PT() {
		int cbCardType[][] = new int[4][3];
		int vecReadyCard[][][] = new int[4][3][5];
		for (int i = 0; i < 4; i++) {
			vecReadyCard[i][0][0] = m_cbHandCardData[i][0];
			vecReadyCard[i][0][1] = m_cbHandCardData[i][1];
			vecReadyCard[i][0][2] = m_cbHandCardData[i][2];
			m_GameLogic.SortCardList(vecReadyCard[i][0], 3);
			cbCardType[i][0] = m_GameLogic.GetCardType(vecReadyCard[i][0], 3);

			vecReadyCard[i][1][0] = m_cbHandCardData[i][3];
			vecReadyCard[i][1][1] = m_cbHandCardData[i][4];
			vecReadyCard[i][1][2] = m_cbHandCardData[i][5];
			vecReadyCard[i][1][3] = m_cbHandCardData[i][6];
			vecReadyCard[i][1][4] = m_cbHandCardData[i][7];
			m_GameLogic.SortCardList(vecReadyCard[i][1], 5);
			cbCardType[i][1] = m_GameLogic.GetCardType(vecReadyCard[i][1], 5);

			vecReadyCard[i][2][0] = m_cbHandCardData[i][8];
			vecReadyCard[i][2][1] = m_cbHandCardData[i][9];
			vecReadyCard[i][2][2] = m_cbHandCardData[i][10];
			vecReadyCard[i][2][3] = m_cbHandCardData[i][11];
			vecReadyCard[i][2][4] = m_cbHandCardData[i][12];
			m_GameLogic.SortCardList(vecReadyCard[i][2], 5);
			cbCardType[i][2] = m_GameLogic.GetCardType(vecReadyCard[i][2], 5);
		}

		int roundTmpScore[] = new int[4];
		int roundSuanFen[][][] = new int[4][4][3];

		int cntUser = 0;
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntUser++;
			}
		}
		// 通比模式
		if (m_zhuangMode.equalsIgnoreCase("WUZHUANG")) {
			for (int i = 0; i < 4; i++) {
				int byNext = i;
				if (vecReadyCard[i][0][0] == 0) {
					continue;
				}
				for (int j = 0; j < 3; j++) {
					byNext = GetNextUser(byNext);
					if (vecReadyCard[byNext][0][0] == 0) {
						continue;
					}
					int curScore = 0;
					int prizeScore = 0;
					int cntWin = 0;
					int cntLose = 0;
					for (int k = 0; k < 3; k++) {
						int suanfen = 0;
						int ret = m_GameLogic.CompareCard(vecReadyCard[i][k], vecReadyCard[byNext][k], 5);

						if (k == 0) {
							if (ret == 2) {
								cntWin++;

								curScore += 1;
								suanfen = 1;

							}
							if (ret == 1) {
								cntLose++;

								curScore += -1;
								suanfen = -1;

							}
							if (ret == 0) {
								curScore = 0;
							}
						}

						if (k == 1) {
							if (ret == 2) {
								cntWin++;

								curScore += 1;
								suanfen = 1;

							}
							if (ret == 1) {
								cntLose++;

								curScore += -1;
								suanfen = -1;

							}
							if (ret == 0) {
								curScore = 0;
							}
						}

						if (k == 2) {
							if (ret == 2) {
								cntWin++;

								curScore += 1;
								suanfen = 1;

							}
							if (ret == 1) {
								cntLose++;

								curScore += -1;
								suanfen = -1;

							}
							if (ret == 0) {
								curScore = 0;
							}
						}

						roundSuanFen[i][byNext][k] = suanfen;

					}

					roundTmpScore[i] += curScore;

				}
				
			}
			
			
			//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			
			for(int i=0;i<4;i++){
				int byNext = i;
				if (vecReadyCard[i][0][0] == 0) {
					continue;
				}
			
				int curScore = 0;
				int prizeScore = 0;
				int cntWin = 0;
				int cntLose = 0;
				
				if(m_GameLogic.getHandCardType(m_bTableCardArray[i], 13) == Constants.CT_ZHIZUNQINGLONG){//至尊青龙
					
					for(int j=0;j<3;j++){
						
						byNext = GetNextUser(byNext);
						if (vecReadyCard[byNext][0][0] == 0) {
							continue;
						}
						if(m_GameLogic.getHandCardType(m_bTableCardArray[byNext], 13) != Constants.CT_ZHIZUNQINGLONG){
							curScore +=52;
							roundTmpScore[i] += curScore;
							roundTmpScore[byNext] -= curScore;
						}
						
					}
					
					
				}else if(m_GameLogic.getHandCardType(m_bTableCardArray[i], 13) == Constants.CT_YITIAOLONG){//一条龙
                    for(int j=0;j<3;j++){
						
						byNext = GetNextUser(byNext);
						if (vecReadyCard[byNext][0][0] == 0) {
							continue;
						}
						if(m_GameLogic.getHandCardType(m_bTableCardArray[byNext], 13) != Constants.CT_YITIAOLONG){
							curScore +=26;
							roundTmpScore[i] += curScore;
							roundTmpScore[byNext] -= curScore;
						}
						
					}
					
				}else if(m_GameLogic.getHandCardType(m_bTableCardArray[i], 13) == Constants.CT_SITAOSANTIAO){//四条三套
					
					  for(int j=0;j<3;j++){
							
							byNext = GetNextUser(byNext);
							if (vecReadyCard[byNext][0][0] == 0) {
								continue;
							}
							if(m_GameLogic.getHandCardType(m_bTableCardArray[byNext], 13) != Constants.CT_SITAOSANTIAO){
								curScore +=6;
								roundTmpScore[i] += curScore;
								roundTmpScore[byNext] -= curScore;
							}
							
						}
				}else if(m_GameLogic.getHandCardType(m_bTableCardArray[i], 13) == Constants.CT_LIUDUIBAN){//六对半
					  for(int j=0;j<3;j++){
							
							byNext = GetNextUser(byNext);
							if (vecReadyCard[byNext][0][0] == 0) {
								continue;
							}
							if(m_GameLogic.getHandCardType(m_bTableCardArray[byNext], 13) != Constants.CT_LIUDUIBAN){
								curScore +=6;
								roundTmpScore[i] += curScore;
								roundTmpScore[byNext] -= curScore;
							}
							
						}
				}
		
			
			}
			

			
			for(int i=0;i<4;i++){
				
				int byNext = i;
				if (vecReadyCard[i][0][0] == 0) {
					continue;
				}
				
				int shunzi = 0;
				int tonghua = 0;
				int curScore = 0;
				
				for(int j=0;j<3;j++){
					
					
					int type = m_GameLogic.GetCardType(vecReadyCard[i][j],vecReadyCard[i][j].length);
					if((type == Constants.CT_KING_TONG_HUA_SHUN)||(type == Constants.CT_TONG_HUA_SHUN)||(type == Constants.CT_SHUN_ZI)){
						shunzi ++;
					}
					if((type == Constants.CT_KING_TONG_HUA_SHUN)||(type == Constants.CT_TONG_HUA_SHUN)||(type == Constants.CT_TONG_HUA)){
						tonghua ++;
					}
				}
				
				if(shunzi == 3&&tonghua != 3){
					curScore +=6;
					for(int k=0;k<3;k++){
						byNext = GetNextUser(byNext);
						
					}
					roundTmpScore[i] += curScore;
					roundTmpScore[byNext] -= curScore;
					
				}else if(tonghua == 3&&shunzi != 3){
					curScore +=6;
					for(int k=0;k<3;k++){
						byNext = GetNextUser(byNext);
						
					}
					roundTmpScore[i] += curScore;
					roundTmpScore[byNext] -= curScore;
				}else if(tonghua == 3&&shunzi == 3){
					curScore +=6;
					for(int k=0;k<3;k++){
						byNext = GetNextUser(byNext);
						
					}
					roundTmpScore[i] += curScore;
					roundTmpScore[byNext] -= curScore;
				}
				
				
			}
			
			
		} else {
			if (true) {
				int byNext = m_byZhuang;

				for (int j = 0; j < 3; j++) {
					byNext = GetNextUser(byNext);
					if (vecReadyCard[byNext][0][0] == 0) {
						continue;
					}
					int curScore = 0;
					int prizeScore = 0;
					int cntWin = 0;
					int cntLose = 0;
					for (int k = 0; k < 3; k++) {
						int suanfen = 0;
						int ret = m_GameLogic.CompareCard(vecReadyCard[m_byZhuang][k], vecReadyCard[byNext][k], 5);

						if (k == 0) {
							if (ret == 2) {
								cntWin++;

								curScore += 1;
								suanfen = 1;

							}
							if (ret == 1) {
								cntLose++;

								curScore += -1;
								suanfen = -1;

							}
							if (ret == 0) {
								curScore = 0;
							}
						}

						if (k == 1) {
							if (ret == 2) {
								cntWin++;

								curScore += 1;
								suanfen = 1;

							}
							if (ret == 1) {
								cntLose++;

								curScore += -1;
								suanfen = -1;

							}
							if (ret == 0) {
								curScore = 0;
							}
						}

						if (k == 2) {
							if (ret == 2) {
								cntWin++;

								curScore += 1;
								suanfen = 1;

							}
							if (ret == 1) {
								cntLose++;

								curScore += -1;
								suanfen = -1;

							}
							if (ret == 0) {
								curScore = 0;
							}
						}

						roundSuanFen[m_byZhuang][byNext][k] = suanfen;
						roundSuanFen[byNext][m_byZhuang][k] = 0 - suanfen;

					}
					roundTmpScore[m_byZhuang] += curScore;
					roundTmpScore[byNext] -= curScore;					
				}
			}

		}

		for (int i = 0; i < 4; i++) {
			m_vecRoundScore[i] = roundTmpScore[i];
		}
		SUBCMD_S_SuanFen cmd = new SUBCMD_S_SuanFen();

		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				for (int k = 0; k < 3; k++) {
					cmd.nScore[i][j][k] = roundSuanFen[i][j][k];
				}
			}
		}

		String sData = cmd.getJsonData();
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				SendData(m_pIUserItem[i].dwUserID, sData);
			}
		}

		OnEventGameEnd(Constants.INVALID_CHAIR, Constants.GER_NORMAL);
	}

	// 就绪事件
	protected boolean OnUserReady(int wChairID) {
		if (wChairID >= 4) {
			System.out.printf("Error wChairID (%d)\n", wChairID);
			return false;
		}
		m_wRobotSeconds[wChairID] = 0;
		// 状态判断
		if (m_bGameStarted)
			return false;

		m_pIUserItem[wChairID].cbUserStatus = US_READY;

		SUBCMD_S_Ready cmd = new SUBCMD_S_Ready();
		cmd.m_lUid = m_pIUserItem[wChairID].dwUserID;
		cmd.m_wUserChairID = (short) wChairID;// 玩家座位号

		String sData = cmd.getJsonData();
		SendData(Constants.INVALID_CHAIR, sData);

		return false;
	}

	void OnEventGameStart() {
		Init(Constants.INIT_NEWROUND);

		m_curStage = "STAGE_START";

		SUBCMD_S_Start cmd = new SUBCMD_S_Start();
		cmd.byRound = m_byRound;
		cmd.byMaxRound = m_byMaxRound;
		for (int i = 0; i < 4; i++) {
			m_vecRoundScore[i] = 0;
			m_teShuPaiList[i] = 0;
			if (m_pIUserItem[i].dwUserID > 0 && (m_pIUserItem[i].cbUserStatus == Constants.US_READY
					|| m_pIUserItem[i].cbUserStatus == Constants.US_PLAY)) {
				m_pIUserItem[i].cbUserStatus = Constants.US_PLAY;
				cmd.vecIsReady[i] = 1;
			} else {
				m_pIUserItem[i].cbUserStatus = Constants.US_NULL;
				cmd.vecIsReady[i] = 0;
			}
		}

		String sData = cmd.getJsonData();
		SendData(Constants.INVALID_CHAIR, sData);

		if (m_zhuangMode.equalsIgnoreCase("WUZHUANG")) {
			FaPai();
		} else {
			needDealTime = 0;
			if (m_zhuangMode.equalsIgnoreCase("LUNZHUANG") || m_zhuangMode.equalsIgnoreCase("DINGZHUANG")) {
				needDealTime = 2;
			}
			if (m_zhuangMode.equalsIgnoreCase("QIANGZHUANG")) {
				needDealTime = 8;
			}
		}

		m_bTableStarted = true;
		m_bGameStarted = true;
	}
	void OnEventGameEnd(int bySeatID,int cbReason){
		if(isZuJuType()){
			OnEventGameEndZuJu(bySeatID,cbReason);
		}else{
			onEventGameEndGold(bySeatID,cbReason);
		}
	}
	void onEventGameEndGold(int bySeatID, int cbReason) {
		m_byRound = m_byRound + 1;
		OnRoundInfoChange();
		m_curStage = "STAGE_JIESUAN";
		needDealTime = 10;

		m_bGameStarted = false;

		ConcludeGame();
		int cntUser = 0;
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0) {
				cntUser++;
			}
		}

		if (cbReason != Constants.GAME_END_DISMISS) {
			double shuishourate = getShuiShou(this.GetType());
			int shui = (int) (this.m_difen * shuishourate);

			//扣税
			for(int i = 0;i < 4;i++){
				if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
					continue;
				}
				SecShmUser user = dthall.getInstance().getSecurityUserInfo(m_pIUserItem[i].dwUserID);
				//扣除税收先,在扣除UU逗的时候，最多扣完,不会扣成负数(系统不补贴)
				m_pIUserItem[i].lUserCoin -= shui;
				user.getCoin(0 - shui);
				UserGoldChange.handleGoldChangeKouShui(user, shui, UserGoldChange.reason23);
			}
			
			//统计总的输赢，跟用户可以的输赢，按照比例换算,系统不补贴
			int userCanLoseCoin = 0;
			int userRealWinCoin = 0;
			for(int i = 0;i < 4;i++){
				m_vecRoundScore[i] *= m_difen;
			}
			for(int i = 0;i < 4;i++){
				if(m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus){
					continue;
				}
				if(m_vecRoundScore[i] > 0){
					userRealWinCoin += m_vecRoundScore[i];
				}else if(m_vecRoundScore[i] < 0){
					userCanLoseCoin += m_pIUserItem[i].lUserCoin;					
				}
			}
			if(userRealWinCoin > userCanLoseCoin){
				float rate = (float) (userCanLoseCoin * 1.0 / userRealWinCoin);
				for(int i = 0;i < 4;i++){
					if(m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus){
						continue;
					}
					if(m_vecRoundScore[i] > 0){
						m_vecRoundScore[i] = (int) (m_vecRoundScore[i] * rate);
					}else if(m_vecRoundScore[i] < 0){
						m_vecRoundScore[i] = (int) (m_vecRoundScore[i] * rate);
						if(Math.abs(m_vecRoundScore[i]) > m_pIUserItem[i].lUserCoin){
							m_vecRoundScore[i] = - m_pIUserItem[i].lUserCoin;
						}
					}
				}				
			}
			//记录
			for(int i = 0;i < 4;i++){
				if(m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus){
					continue;
				}
				SecShmUser xianUser = dthall.getInstance().getSecurityUserInfo(m_pIUserItem[i].dwUserID);
				//记录
				m_pIUserItem[i].lUserCoin += m_vecRoundScore[i];
				xianUser.getCoin(m_vecRoundScore[i]);

				UserGoldChange.handleGoldChangeWinLose(xianUser,m_vecRoundScore[i],UserGoldChange.reason27);
			}
			for (int i = 0; i < 4; i++) {
				m_pIUserItem[i].lUserScore += m_vecRoundScore[i];
				m_vecResultScore[i] += m_vecRoundScore[i];
			}

			{
				SUBCMD_S_JieSuan cmd = new SUBCMD_S_JieSuan();
				cmd.lShuiShou = shui;
				for(int i = 0;i < 4;i++){
					if(m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus){
						cmd.lUserCoin[i] = 0;
						continue;
					}
					cmd.lUserCoin[i] = m_pIUserItem[i].lUserCoin;
				}
				cmd.byRound = m_byRound;
				for (int i = 0; i < 4; i++) {
					cmd.vecIsReady[i] = m_vecIsKaiPaiReady[i];
					Constants.CopyMemory(cmd.byPai[i], m_cbHandCardData[i], 13);
				}
				for (int i = 0; i < 4; i++) {
					cmd.nRoundScore[i] = m_vecRoundScore[i];
					cmd.nUserScore[i] = m_pIUserItem[i].lUserScore;
				}
				SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

				for (int i = 0; i < 4; i++) {// 清除牌局数据
					m_vecRoundScore[i] = 0;
				}
			}
		}

		if (m_byRound <= 1 && cbReason == Constants.GAME_END_DISMISS) {
			SUBCMD_S_DismissRoom cmd = new SUBCMD_S_DismissRoom();
			cmd.lUid = 0;
			cmd.cbResult = 0;
			cmd.m_wUserChairID = 0;
			cmd.m_dwTableID = m_dwTableID;

			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

			DelayRemove();
			QpGameMgr.getInstance().OnDismissRoom(this);

		} else if (m_byRound > m_byMaxRound || cbReason == Constants.GAME_END_DISMISS) {	
			for(int i1 = 0;i1 < 4;i1++){
				m_vecDaQiangTime[i1] += m_vecRoundDaQiangTime[i1];
				m_vecBeiDaTime[i1] += m_vecRoundBeiDaTime[i1];
				m_vecQuanLeiTime[i1] += m_vecRoundQuanLeiTime[i1];
			}
			for (int i1 = 0; i1 < 4; i1++) {
				m_vecRoundDaQiangTime[i1] = 0;
				m_vecRoundBeiDaTime[i1] = 0;
				m_vecRoundQuanLeiTime[i1] = 0;
			}
			SUBCMD_S_REPORT cmd = new SUBCMD_S_REPORT();
			cmd.cbReason = cbReason;
			cmd.whoDismiss = 0;

			for (int k = 0; k < 4; k++) {
				cmd.vecWinScore[k] = m_vecWinScore[k];
				cmd.vecLoseScore[k] = m_vecLoseScore[k];
				cmd.vecResultScore[k] = m_vecResultScore[k];
				cmd.vecDaQiangTime[k] = m_vecDaQiangTime[k];
				cmd.vecBeiDaTime[k] = m_vecBeiDaTime[k];
				cmd.vecQuanLeiTime[k] = m_vecQuanLeiTime[k];
			}

			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

			DelayRemove();

			QpGameMgr.getInstance().OnDismissRoom(this);
		}

	}
	void OnEventGameEndZuJu(int bySeatID, int cbReason) {
		m_byRound = m_byRound + 1;
		OnRoundInfoChange();
		m_curStage = "STAGE_JIESUAN";
		needDealTime = 10;

		m_bGameStarted = false;

		ConcludeGame();
		int cntUser = 0;
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0) {
				cntUser++;
			}
		}

		if (cbReason != Constants.GAME_END_DISMISS) {
			for (int i = 0; i < 4; i++) {
				m_pIUserItem[i].lUserScore += m_vecRoundScore[i];
				m_vecResultScore[i] += m_vecRoundScore[i];
			}

			{
				SUBCMD_S_JieSuan cmd = new SUBCMD_S_JieSuan();
				cmd.byRound = m_byRound;
				for (int i = 0; i < 4; i++) {
					cmd.vecIsReady[i] = m_vecIsKaiPaiReady[i];
					Constants.CopyMemory(cmd.byPai[i], m_cbHandCardData[i], 13);
				}
				for (int i = 0; i < 4; i++) {
					cmd.nRoundScore[i] = m_vecRoundScore[i];
					cmd.nUserScore[i] = m_pIUserItem[i].lUserScore;
				}
				SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

				for (int i = 0; i < 4; i++) {// 清除牌局数据
					m_vecRoundScore[i] = 0;
				}
			}
		}

		if (m_byRound <= 1 && cbReason == Constants.GAME_END_DISMISS) {
			SUBCMD_S_DismissRoom cmd = new SUBCMD_S_DismissRoom();
			cmd.lUid = 0;
			cmd.cbResult = 0;
			cmd.m_wUserChairID = 0;
			cmd.m_dwTableID = m_dwTableID;

			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

			DelayRemove();
			QpGameMgr.getInstance().OnDismissRoom(this);

		} else if (m_byRound > m_byMaxRound || cbReason == Constants.GAME_END_DISMISS) {
			int before_cost[] = new int[4];
			int after_cost[] = new int[4];

			int cost[] = new int[4];			
			for(int i=0;i<4;i++)
			{
				before_cost[i] = 0;
				cost[i] = 0;
				after_cost[i] = 0;
			}
			
			for (int i = 0; i < 4; i++) 
			{				
				if(m_pIUserItem[i].dwUserID > 0)
				{
					SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[i].dwUserID);
					if(user != null)
					{
						before_cost[i] = user.getGem(0);
					}
				}
			}	
			if (m_byRound > 2) {
				if (m_payWay == "FZPAY") {
					int cost_gem = 0;
					for (int i = 0; i < 4; i++) {
						if (m_pIUserItem[i].dwUserID > 0) {
							cost_gem += m_byCostGem;
						}
					}
					if (m_Creator > 0) {
						SHMUser user = dthall.getInstance().getUserInfo(m_Creator);
						if (user != null) {
							user.getGem(0 - m_byCostGem);
						}
					} else {
						SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[0].dwUserID);
						if (user != null) {
							user.getGem(0 - cost_gem);
						}
						cost[0] = 0 - cost_gem;
					}
				}
				if (m_payWay == "AAPAY") {
					for (int i = 0; i < 4; i++) {
						int lFangZhu = m_pIUserItem[i].dwUserID;
						if (lFangZhu > 0) {
							SHMUser user = dthall.getInstance().getUserInfo(lFangZhu);
							if (user != null) {
								user.getGem(0 - m_byCostGem);
								cost[i] = 0 - m_byCostGem;
							}
						}
					}
				}
				if (m_payWay.equalsIgnoreCase("WINPAY")) {
					int maxUser = 4;
					int activeUser = 0;
					for(int i = 0;i < maxUser;i++){
						if (m_pIUserItem[i].dwUserID > 0){
							activeUser++;
						}
					}
					int maxcost = m_byCostGem * activeUser;
					List<Integer> winuser = new ArrayList<Integer>();
					int max = -1;
					for (int i = 0; i < maxUser; i++) {
						if (m_vecResultScore[i] > max) {
							max = m_vecResultScore[i];
						}
					}
					for (int i = 0; i < maxUser; i++) {
						if (m_vecResultScore[i] == max) {
							winuser.add(i);
						}
					}
					float rate = m_byRound * 1.0f / m_byMaxRound;
					if(rate > 1.0) rate = 1.0f;
					rate *= maxcost;
					Float frate = new Float(rate);
					int cost_gem = frate.intValue();
					cost_gem /= winuser.size();

					for (int i = 0; i < maxUser; i++) {
						if (m_pIUserItem[i].dwUserID > 0 && winuser.indexOf(i) != -1) {
							SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[i].dwUserID);
							if (user != null) {
								user.getGem(0 - cost_gem);
								cost[i] = 0 - cost_gem;
							}
						}
					}
				}
				
				for (int i = 0; i < 4; i++) 
				{				
					if(m_pIUserItem[i].dwUserID > 0)
					{
						SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[i].dwUserID);
						if(user != null)
						{
							after_cost[i] = user.getGem(0);
						}
					}
				}
			}

			for(int i1 = 0;i1 < 4;i1++){
				m_vecDaQiangTime[i1] += m_vecRoundDaQiangTime[i1];
				m_vecBeiDaTime[i1] += m_vecRoundBeiDaTime[i1];
				m_vecQuanLeiTime[i1] += m_vecRoundQuanLeiTime[i1];
			}
			for (int i1 = 0; i1 < 4; i1++) {
				m_vecRoundDaQiangTime[i1] = 0;
				m_vecRoundBeiDaTime[i1] = 0;
				m_vecRoundQuanLeiTime[i1] = 0;
			}
			SUBCMD_S_REPORT cmd = new SUBCMD_S_REPORT();
			cmd.cbReason = cbReason;
			cmd.whoDismiss = 0;

			for (int k = 0; k < 4; k++) {
				cmd.vecWinScore[k] = m_vecWinScore[k];
				cmd.vecLoseScore[k] = m_vecLoseScore[k];
				cmd.vecResultScore[k] = m_vecResultScore[k];
				cmd.vecDaQiangTime[k] = m_vecDaQiangTime[k];
				cmd.vecBeiDaTime[k] = m_vecBeiDaTime[k];
				cmd.vecQuanLeiTime[k] = m_vecQuanLeiTime[k];
			}

			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

			log_counter++;
			log_counter = log_counter % 100;
			Long t = System.currentTimeMillis() / 1000;
			Long battle_id = (t % 1000000) * 100 + log_counter;

			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String created = df.format(new Date());
			{

				JSONObject root = new JSONObject();

				root.put("command", "save_battle_log");
				root.put("battle_id", battle_id);

				root.put("room_type", GetType());

				root.put("room_id", m_dwTableID);
				root.put("creator", GetCreator());

				root.put("jushu", m_byMaxRound);
				root.put("limitscore", 0);

				if (m_payWay == "WINPAY") {
					root.put("payway", 1);
				} else if (m_payWay == "BIGWINPAY") {
					root.put("payway", 2);
				} else {
					root.put("payway", 0);
				}
				root.put("created", created);
				root.put("uid0", (int) m_pIUserItem[0].dwUserID);
				root.put("nickname0", m_pIUserItem[0].szNickName);
				root.put("score0", m_vecResultScore[0]);
				root.put("cost0", cost[0]);
				root.put("uid1", (int) m_pIUserItem[1].dwUserID);
				root.put("nickname1", m_pIUserItem[1].szNickName);
				root.put("score1", m_vecResultScore[1]);
				root.put("cost1", cost[1]);
				root.put("uid2", (int) m_pIUserItem[2].dwUserID);
				root.put("nickname2", m_pIUserItem[2].szNickName);
				root.put("score2", m_vecResultScore[2]);
				root.put("cost2", cost[2]);
				root.put("uid3", (int) m_pIUserItem[3].dwUserID);
				root.put("nickname3", m_pIUserItem[3].szNickName);
				root.put("score3", m_vecResultScore[3]);
				root.put("cost3", cost[3]);
				root.put("uid4", 0);
				root.put("nickname4", "");
				root.put("score4", 0);
				root.put("cost4", 0);

				GameDB.getInstance().push_task(null, root);

				try
				{
					for(int i =0;i<4;i++)
					{
						if(m_pIUserItem[i].dwUserID > 0 && cost[i] != 0)
						{
							JSONObject root_cost = new JSONObject();
							root_cost.put("command", "save_charge_log");
							root_cost.put("created", created);
							root_cost.put("uid", (int) m_pIUserItem[i].dwUserID);
							root_cost.put("before_cost", (int) before_cost[i]);
							root_cost.put("after_cost", (int) after_cost[i]);
							root_cost.put("cost", (int) cost[i]);
							root_cost.put("reason", "用户" + m_pIUserItem[0].dwUserID + "创建  十三水   " + m_byMaxRound + "局 " + m_payWay + " " );
							root_cost.put("room_id", m_dwTableID);
							
							GameDB.getInstance().push_task(null, root_cost);
						}
					}	
				}catch(Exception e)
				{
					e.printStackTrace();
				}
			}

			m_bTableStarted = false;
			for (int i = 0; i < 4; i++) {// 清除牌局数据
				m_vecResultScore[i] = 0;
			}

			DelayRemove();

			QpGameMgr.getInstance().OnDismissRoom(this);


			//处理积分
			if(m_byRound == m_byMaxRound){
				int score = 1;
				for(int i = 0;i < 4;i++){
					if(m_pIUserItem[i].dwUserID > 0){
						int uid = m_pIUserItem[i].dwUserID;
						UserActionActivity activity = ActivityMgr.getInstance().handleSendHuaFeiActionByPlay(uid,score,"*");
						if(activity.m_useActioned){
							RankMgr.getInstance().addUserRankScore(uid,activity.m_expGet);
						}
					}
				}
			}
		}

	}

	protected boolean ConcludeGame() {
		// 设置状态

		RepositTable();

		for (int i = 0; i < 4; ++i) {
			if (m_pIUserItem[i].dwUserID > 0) {
				m_pIUserItem[i].cbUserStatus = US_NULL;
			}
		}

		return true;
	}

	// 复位桌子
	public void RepositTable() {
		// 状态变量
		m_bGameStarted = false;

	}

	static int log_counter = 0;

	void OnDealQiangZhuang() {

		m_curStage = "STAGE_QIANGZHUANG";
		needDealTime = 3;

		List<Integer> vecQiangZhuang = new ArrayList<>();
		List<Integer> vecTmpUser = new ArrayList<>();
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				if (m_byQiangZhuang[i] > 0) {
					vecQiangZhuang.add(i);
				}
				vecTmpUser.add(i);
			}
		}
		int byQiangZhuang[] = new int[5];
		SUBCMD_S_DingZhuang cmd = new SUBCMD_S_DingZhuang();
		cmd.lUid = 0;
		cmd.wChairID = 0;
		cmd.wPower = 0;
		if (vecQiangZhuang.size() > 0) {
			int byZhuang = vecQiangZhuang.get(Constants.rand() % vecQiangZhuang.size());
			if (m_byQiangZhuang[byZhuang] == 0) {
				m_byQiangZhuang[byZhuang] = 1;
			}
			cmd.lUid = m_pIUserItem[byZhuang].dwUserID;
			cmd.wChairID = byZhuang;
			cmd.wPower = m_byQiangZhuang[byZhuang];

			for (int i = 0; i < vecQiangZhuang.size(); i++) {
				byQiangZhuang[vecQiangZhuang.get(i)] = m_byQiangZhuang[vecQiangZhuang.get(i)];
			}
			m_byZhuang = byZhuang;
		} else {
			if (vecTmpUser.size() > 0) {
				int byZhuang = vecTmpUser.get(Constants.rand() % vecTmpUser.size());
				if (m_byQiangZhuang[byZhuang] == 0) {
					m_byQiangZhuang[byZhuang] = 1;
				}
				for (int i = 0; i < 4; i++) {
					if (m_pIUserItem[i].dwUserID > 0) {
					} else {
						m_byQiangZhuang[i] = 0;
					}
				}
				cmd.lUid = m_pIUserItem[byZhuang].dwUserID;
				cmd.wChairID = byZhuang;
				cmd.wPower = m_byQiangZhuang[byZhuang];

				for (int i = 0; i < vecTmpUser.size(); i++) {
					byQiangZhuang[vecTmpUser.get(i)] = m_byQiangZhuang[vecTmpUser.get(i)];
					if (byQiangZhuang[vecTmpUser.get(i)] < 0 || byQiangZhuang[vecTmpUser.get(i)] > 250) {
						byQiangZhuang[vecTmpUser.get(i)] = 1;
					}
				}
				m_byZhuang = byZhuang;
			} else {
				return;
			}

		}
		for (int i = 0; i < 4; i++) {
			cmd.byQiangZhuang[i] = byQiangZhuang[i];
		}

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

	}

	void QiangZhuang(int byZhuang,int delay_time) {
		m_curStage = "STAGE_QIANGZHUANG";
		needDealTime = delay_time;

		SUBCMD_S_DingZhuang cmd = new SUBCMD_S_DingZhuang();
		cmd.lUid = 0;
		cmd.wChairID = 0;
		cmd.wPower = 0;

		if (m_byQiangZhuang[byZhuang] == 0) {
			m_byQiangZhuang[byZhuang] = 1;
		}
		cmd.lUid = m_pIUserItem[byZhuang].dwUserID;
		cmd.wChairID = byZhuang;
		cmd.wPower = m_byQiangZhuang[byZhuang];

		m_byZhuang = byZhuang;

		cmd.byQiangZhuang[byZhuang] = 1;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}

	public static long currDealTime = 0;
	void OnDealAI(int dt) {
		m_DealTime = (int) (System.currentTimeMillis() / 1000);
		
		if(currDealTime == 0){
			currDealTime = System.currentTimeMillis();
		}else{
			long time = System.currentTimeMillis();
			if(time - currDealTime >= 1000){
				currDealTime = time;
			}else{
				return;
			}
		}
		if (m_wFaPaiDelay > 0) {
			m_wFaPaiDelay--;
			return;
		}
		int cntUser = 0;
		for (int wChairID = 1; wChairID < 4; wChairID++) {
			if (m_pIUserItem[wChairID].dwUserID > 0) {
				cntUser++;
			}
		}
		if (m_curStage == "STAGE_START") {
			if (m_zhuangMode.equalsIgnoreCase("QIANGZHUANG")) {
				QiangZhuang(m_byZhuang,3);
			} else if (m_zhuangMode.equalsIgnoreCase("LUNZHUANG")) {
				int byNextUser = GetNextUser(m_byZhuang);
				QiangZhuang(byNextUser,1);
			} else if (m_zhuangMode.equalsIgnoreCase("DINGZHUANG")) {
				QiangZhuang(m_byZhuang,1);
			} else {

			}
		} else if (m_curStage == "STAGE_QIANGZHUANG") {
			FaPai();
		} else if (m_curStage == "STAGE_FAPAI") {

		}
	}

	public void OnDeal(int dt) {
		if (m_bTableStarted == true) {
			if (needDealTime > 0) {
				needDealTime--;
				return;
			}
			if (m_wFaPaiDelay > 0) {
				m_wFaPaiDelay--;
				return;
			}
			if (m_bGameStarted) {
				OnDealAI(dt);
			} else {
				// OnEventGameStart();
			}
		}
	}

	static int randRobotIndex = 999;

	void AddRobot() {
		randRobotIndex++;
		int robot_uid = randRobotIndex % 9999 + 200000;

		Vector<Integer> vecEmpty = new Vector<>();
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID == 0) {
				vecEmpty.add(i);

			}
		}
		if (vecEmpty.size() > 0) {
			int rand = Constants.rand(0, 10000);
			int wChairID = vecEmpty.get(Constants.rand() % vecEmpty.size());

			int vecMaleFace[] = { 1, 3, 5, 7, 9 };
			int vecFemaleFace[] = { 2, 4, 6, 8, 10 };
			m_pIUserItem[wChairID].dwUserID = robot_uid;
			m_pIUserItem[wChairID].lUserScore = 0;
			m_pIUserItem[wChairID].wChairID = wChairID;
			m_pIUserItem[wChairID].cbGender = rand % 2;
			if (m_pIUserItem[wChairID].cbGender == 1) {
				m_pIUserItem[wChairID].dwFaceID = vecMaleFace[rand % 4];
			} else {
				m_pIUserItem[wChairID].dwFaceID = vecFemaleFace[rand % 4];
			}
			m_pIUserItem[wChairID].cbUserStatus = Constants.US_NULL;
			m_pIUserItem[wChairID].isAndroidUser = 1;

			m_pIUserItem[wChairID].szNickName = "USER[" + wChairID + "]";

			{
				SUBCMD_S_Sit cmd = new SUBCMD_S_Sit();
				// memset(&cmd, 0, sizeof(cmd));

				cmd.m_lUid = robot_uid;
				cmd.m_wUserChairID = m_pIUserItem[wChairID].wChairID;
				cmd.m_lUserScore = m_pIUserItem[wChairID].lUserScore;
				cmd.m_iAvatar = m_pIUserItem[wChairID].dwFaceID;
				cmd.szNickName = m_pIUserItem[wChairID].szNickName;
				cmd.szIP = "";

				SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
			}
			{
				m_pIUserItem[wChairID].cbUserStatus = Constants.US_READY;

				SUBCMD_S_Ready cmd = new SUBCMD_S_Ready();
				// memset(&cmd, 0, sizeof(cmd));

				cmd.m_lUid = robot_uid;
				cmd.m_wUserChairID = m_pIUserItem[wChairID].wChairID;

				SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
			}
			m_wRobotSeconds[wChairID] = 0;
		}
	}

	void Handle_onJoin(int _Uid, String vecArg[]) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID < 4) {
			m_pIUserItem[bySeatID].cbOnline = Constants.US_NULL;
		}

		SUBCMD_S_Join cmd = new SUBCMD_S_Join();
		// memset(&cmd, 0, sizeof(cmd));

		String sData = cmd.getJsonData();
		SendData(_Uid, sData);
	}

	void Handle_onReady(int _Uid, String vecArg[]) {
		if (m_bGameStarted == true) {
			return;
		}
		int bySeatID = FindSeat(_Uid);
		if (bySeatID < 4) {
			m_pIUserItem[bySeatID].cbUserStatus = Constants.US_READY;
		}
		int cntReady = 0;
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].cbUserStatus == Constants.US_READY) {
				cntReady++;
			}
		}
		int maxUser = 0;
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				maxUser++;
			}
		}
		SUBCMD_S_Ready cmd = new SUBCMD_S_Ready();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = bySeatID;
		cmd.m_dwTableID = m_dwTableID;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
		if (cntReady > 1&& cntReady == maxUser) {
			OnEventGameStart();
		} else {

		}
	}

	void Handle_onRobot(int _Uid, String vecArg[]) {
		if (m_bGameStarted == true) {
			return;
		}
		int bySeatID = FindSeat(_Uid);
		if (bySeatID < 4) {
			m_pIUserItem[bySeatID].cbUserStatus = Constants.US_READY;
		}
		int cntPlayer = 0;
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntPlayer++;
			}
		}
		if (cntPlayer < 4) {
			for (int i = 0; i < 4 - cntPlayer; i++) {
				AddRobot();
			}
		}
	}

	// 表情
	void Handle_onBiaoQing(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, wChairID);
			return;
		}
		if (vecArg.length < 2)
			return;
		int index = Constants.atoi(vecArg[1]);

		SUBCMD_S_BiaoQing cmd = new SUBCMD_S_BiaoQing();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.m_iBiaoQingIndex = index;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}

	// 语音
	void Handle_onYuYin(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, wChairID);
			return;
		}
		if (vecArg.length < 2)
			return;
		int index = Constants.atoi(vecArg[1]);

		SUBCMD_S_YuYin cmd = new SUBCMD_S_YuYin();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.m_iYuYinIndex = index;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}

	// 送礼
	void Handle_onSongLi(int _Uid, String vecArg[]) {
	}

	// 聊天
	void Handle_onChat(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, wChairID);
			return;
		}
		if (vecArg.length < 2)
			return;
		String strContent = vecArg[1];

		SUBCMD_S_Chat cmd = new SUBCMD_S_Chat();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.szContent = strContent;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}

	// 语音
	void Handle_onVoice(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, wChairID);
			return;
		}
		if (vecArg.length < 3)
			return;
		int seconds = Constants.atoi(vecArg[1]);
		String strVoiceURL = vecArg[2];

		SUBCMD_S_Voice cmd = new SUBCMD_S_Voice();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.seconds = seconds;
		cmd.szVoiceURL = strVoiceURL;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}
	void Handle_onVoiceMsg(int _Uid, String[] vecArg) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		float seconds = Float.parseFloat(vecArg[1]);
		
		SUBCMD_S_VoiceMsg cmd = new SUBCMD_S_VoiceMsg();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.seconds = seconds;
		
		cmd.data = vecArg[2];
		cmd.m_maxClip = Integer.parseInt(vecArg[3]);
		cmd.m_currClip = Integer.parseInt(vecArg[4]);
		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}
	
	// 文字
	void Handle_onText(int _Uid, String[] vecArg) {

		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		String strContent = vecArg[1];

		strContent = string_replace(strContent, "|", ".");
		strContent = string_replace(strContent, "#", ".");
		strContent = string_replace(strContent, ",", ".");
		strContent = string_replace(strContent, "\\", ".");
		strContent = string_replace(strContent, "'", ".");
		strContent = string_replace(strContent, "\"", ".");

		SUBCMD_S_Text cmd = new SUBCMD_S_Text();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.szContent = strContent;
		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}
	private String string_replace(String strContent, String string, String string2) {
		return strContent.replace(string, string2);
	}
	

	// 语音
	void Handle_onTryDismissRoom(int _Uid, String vecArg[]) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, bySeatID);
			return;
		}
		if (vecArg.length < 2)
			return;

		String attachInfo = vecArg[1];
		if(attachInfo.equals("fzquit")){
			//房主请求直接退出
			OnEventGameEnd(0, Constants.GAME_END_DISMISS);
			DelayRemove();
			QpGameMgr.getInstance().OnDismissRoom(this);
			return;
		}
		int cntUser = 0;
		int cntOnline = 0;
		int cntOffline = 0;
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0) {
				cntUser++;
			}
		}
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0) {
				if (m_pIUserItem[i].cbOnline == Constants.US_OFFLINE) {
					cntOffline++;
				} else {
					cntOnline++;
				}
			}
		}
		if (cntUser > 1) {
			if (m_dismissTimer == 0) {
				m_dismissTimer = Constants.time(null);
			}
			m_byAgreeDismiss[bySeatID] = 1;

			SUBCMD_S_TryDismissRoom cmd = new SUBCMD_S_TryDismissRoom();
			cmd.lUid = _Uid;
			cmd.dwTableID = m_dwTableID;
			cmd.szNickName = m_pIUserItem[bySeatID].szNickName;

			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

			if (cntOnline == 1) {
				OnEventGameEnd(0, Constants.GAME_END_DISMISS);
				DelayRemove();
				QpGameMgr.getInstance().OnDismissRoom(this);

				m_dismissTimer = 0;
			}
		} else {
			OnEventGameEnd(0, Constants.GAME_END_DISMISS);
			DelayRemove();
			QpGameMgr.getInstance().OnDismissRoom(this);
		}

	}

	// 语音
	void Handle_onAgreeDismissRoom(int _Uid, String vecArg[]) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, bySeatID);
			return;
		}
		if (vecArg.length < 2)
			return;
		m_byAgreeDismiss[bySeatID] = 1;

		SUBCMD_S_AgreeDismissRoom cmd = new SUBCMD_S_AgreeDismissRoom();
		cmd.lUid = _Uid;
		cmd.dwTableID = m_dwTableID;
		cmd.szNickName = m_pIUserItem[bySeatID].szNickName;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

		if (bySeatID == 0 && m_bTableStarted == false) {
			OnEventGameEnd(0, Constants.GAME_END_DISMISS);
			DelayRemove();
			QpGameMgr.getInstance().OnDismissRoom(this);
			return;
		}
		int cntAgree = 0;
		for (int i = 0; i < 4; i++) {
			if (m_byAgreeDismiss[i] > 0) {
				cntAgree++;
			}
		}
		int cntUser = 0;
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0) {
				cntUser++;
			}
		}
		if (cntAgree >= 2 && cntAgree > cntUser / 2) {
			OnEventGameEnd(0, Constants.GAME_END_DISMISS);
			DelayRemove();
			QpGameMgr.getInstance().OnDismissRoom(this);

			if (ShouldBeRemoved()) {
				// niuniuTableMgr::instance().DismissTable(this);
			}
		}
	}

	// 语音
	void Handle_onRefuseDismissRoom(int _Uid, String vecArg[]) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, bySeatID);
			return;
		}
		if (vecArg.length < 2)
			return;

		m_dismissTimer = 0;
		m_byAgreeDismiss[0] = 0;
		m_byAgreeDismiss[1] = 0;
		m_byAgreeDismiss[2] = 0;
		m_byAgreeDismiss[3] = 0;
		m_byAgreeDismiss[4] = 0;

		SUBCMD_S_RefuseDismissRoom cmd = new SUBCMD_S_RefuseDismissRoom();
		cmd.lUid = _Uid;
		cmd.dwTableID = m_dwTableID;
		cmd.szNickName = m_pIUserItem[bySeatID].szNickName;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}

	void Handle_onStart(int _Uid, String vecArg[]) {
		if (vecArg.length < 2)
			return;

		int bySeatID = FindSeat(_Uid);
		if (m_bTableStarted == false && bySeatID == 0 && GetPlayerCount() > 1) {
			OnEventGameStart();
		}
	}

	void Handle_onQiangZhuang(int _Uid, String vecArg[]) {
		if (vecArg.length < 3)
			return;

		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, bySeatID);
			return;
		}

		m_byQiangZhuang[bySeatID] = 1;

		int power = 1;

		SUBCMD_S_QiangZhuang cmd = new SUBCMD_S_QiangZhuang();
		cmd.lUid = _Uid;
		cmd.wChairID = bySeatID;
		cmd.wPower = power;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

		int curNum = 0;
		int XuanZhuangN = 0;
		for (int i = 0; i < 4; i++) {
			if (m_byQiangZhuang[i] == 1 || m_byQiangZhuang[i] == -1) {
				XuanZhuangN++;
			}
			if (m_pIUserItem[i].dwUserID > 0) {
				curNum++;
			}
		}
		if (XuanZhuangN >= curNum && m_curStage == "STAGE_START") {
			OnDealQiangZhuang();
		}
	}

	void Handle_onBuQiangZhuang(int _Uid, String vecArg[]) {
		if (vecArg.length < 3)
			return;
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, bySeatID);
			return;
		}
		m_byQiangZhuang[bySeatID] = -1;

		int curNum = 0;
		int XuanZhuangN = 0;
		for (int i = 0; i < 4; i++) {
			if (m_byQiangZhuang[i] == 1 || m_byQiangZhuang[i] == -1) {
				XuanZhuangN++;
			}
			if (m_pIUserItem[i].dwUserID > 0) {
				curNum++;
			}
		}
		if (XuanZhuangN >= curNum && m_curStage == "STAGE_START") {
			needDealTime = 8;
			OnDealQiangZhuang();
		}

	}

	public int FindSeat(int _Uid) {
		for (int bySeatID = 0; bySeatID < 4; bySeatID++) {
			if (m_pIUserItem[bySeatID].dwUserID == _Uid) {
				return bySeatID;
			}
		}
		return Constants.INVALID_CHAIR;
	}

	public int m_DealTime = 0;

	public int getDealTime() {
		return m_DealTime;
	}

	public boolean m_bRemove = false;

	public void DelayRemove() {
		m_bRemove = true;
	}

	public void ResetKillFlag() {
		m_bRemove = false;
	}

	public boolean ShouldBeRemoved() {
		return m_bRemove;
	}

	public void Handle_onLock(Integer lUid, String[] vecArg) {
		if (vecArg.length < 2)
			return;
		if (m_bTableStarted && m_pIUserItem[0].dwUserID == lUid) {// 已经开始 并且是房主
			int index = Constants.atoi(vecArg[1]);
			m_locked = index;
		}
	}

	public void Handle_onKaiPaiReady(Integer lUid, String[] vecArg) {

		if (vecArg.length < 2)
			return;
		int wChairID = FindSeat(lUid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		if(m_vecIsKaiPaiReady[wChairID] == 1){
			return;
		}
		String strTanPai = vecArg[1];
		int dwHandCard[] = new int[13];

		String vecArgTanPai[] = strTanPai.split("\\,");
		if (vecArgTanPai.length != 13) {
			return;
		}

		try {
			for (int i = 0; i < 13; i++) {
				dwHandCard[i] = Integer.parseInt(vecArgTanPai[i]);
			}
			for (int i = 0; i < 13; i++) {
				int cntCard = 0;
				for (int j = 0; j < 13; j++) {
					if (m_bTableCardArray[wChairID][j] == dwHandCard[i]) {
						cntCard++;
					}
				}
				//1, 17, 33, 49, 49, 5, 18, 19, 20, 22, 8, 9, 10
				//10, 9, 8, 22, 5, 20, 19, 18, 33, 17, 1, 49, 49
				if (cntCard != 1&&cntCard != 2) {
					SUBCMD_S_Error cmd = new SUBCMD_S_Error();
					cmd.byRound = m_byRound;
					cmd.bySeatID = wChairID;
					cmd.errCode = 0;
					cmd.errMsg = "系统认定您配牌出错，请仔细检查您的牌";

					String sData = cmd.getJsonData();
					SendData(m_pIUserItem[wChairID].dwUserID, sData);

					return;
				}
			}
			int card0[] = new int[5];
			int card1[] = new int[5];
			int card2[] = new int[5];

			card0[0] = dwHandCard[0];
			card0[1] = dwHandCard[1];
			card0[2] = dwHandCard[2];
			card0[3] = 0;
			card0[4] = 0;
			card1[0] = dwHandCard[0 + 3];
			card1[1] = dwHandCard[1 + 3];
			card1[2] = dwHandCard[2 + 3];
			card1[3] = dwHandCard[3 + 3];
			card1[4] = dwHandCard[4 + 3];
			card2[0] = dwHandCard[0 + 8];
			card2[1] = dwHandCard[1 + 8];
			card2[2] = dwHandCard[2 + 8];
			card2[3] = dwHandCard[3 + 8];
			card2[4] = dwHandCard[4 + 8];
			m_GameLogic.SortCardList(card0, 3);
			m_GameLogic.SortCardList(card1, 5);
			m_GameLogic.SortCardList(card2, 5);
			int ret1 = m_GameLogic.CompareCard(card0, card1, 5);
			int ret2 = m_GameLogic.CompareCard(card1, card2, 5);

			if (ret1 == 2 || ret2 == 2) {
				SUBCMD_S_Error cmd = new SUBCMD_S_Error();
				cmd.byRound = m_byRound;
				cmd.bySeatID = wChairID;
				cmd.errCode = 0;
				cmd.errMsg = "第二道必须大于第一道小于第三道";

				String sData = cmd.getJsonData();
				SendData(m_pIUserItem[wChairID].dwUserID, sData);

				return;
			}
			for (int i = 0; i < 13; i++) {
				m_cbHandCardData[wChairID][i] = dwHandCard[i];
			}
			// System.out.println(IJsonExport.sprintf_BYTE(m_cbHandCardData[wChairID],
			// 13));
		} catch (Exception e) {
			e.printStackTrace();
		}
		////////////////////////////////////////////////
		int cbCardType[][] = new int[4][3];
		int vecReadyCard[][][] = new int[4][3][5];
		for (int i = 0; i < 4; i++) {
			vecReadyCard[i][0][0] = m_cbHandCardData[i][0];
			vecReadyCard[i][0][1] = m_cbHandCardData[i][1];
			vecReadyCard[i][0][2] = m_cbHandCardData[i][2];
			m_GameLogic.SortCardList(vecReadyCard[i][0], 3);
			cbCardType[i][0] = m_GameLogic.GetCardType3Num(vecReadyCard[i][0], 3);

			vecReadyCard[i][1][0] = m_cbHandCardData[i][3];
			vecReadyCard[i][1][1] = m_cbHandCardData[i][4];
			vecReadyCard[i][1][2] = m_cbHandCardData[i][5];
			vecReadyCard[i][1][3] = m_cbHandCardData[i][6];
			vecReadyCard[i][1][4] = m_cbHandCardData[i][7];
			m_GameLogic.SortCardList(vecReadyCard[i][1], 5);
			cbCardType[i][1] = m_GameLogic.GetCardType(vecReadyCard[i][1], 5);

			vecReadyCard[i][2][0] = m_cbHandCardData[i][8];
			vecReadyCard[i][2][1] = m_cbHandCardData[i][9];
			vecReadyCard[i][2][2] = m_cbHandCardData[i][10];
			vecReadyCard[i][2][3] = m_cbHandCardData[i][11];
			vecReadyCard[i][2][4] = m_cbHandCardData[i][12];
			m_GameLogic.SortCardList(vecReadyCard[i][2], 5);
			cbCardType[i][2] = m_GameLogic.GetCardType(vecReadyCard[i][2], 5);
		}
		for(int i=0;i<4;i++){
			int teshu = 0;
			for(int j=0;j<3;j++){
				if(cbCardType[i][j] == Constants.CT_KING_TONG_HUA_SHUN||cbCardType[i][j] == Constants.CT_SHUN_ZI||cbCardType[i][j] == Constants.CT_TONG_HUA_SHUN){
					teshu ++;
				}
			}
			if(teshu == 3){
				if(m_teShuPaiList[i] == 0){
					m_teShuPaiList[i] = Constants.CT_SANSHUNZI;
				}
				
			}
		}
		
		SUBCMD_S_Teshupai cmd1 = new SUBCMD_S_Teshupai();
		cmd1.uid = lUid;
		cmd1.vecTeshupai = m_teShuPaiList;
		String sData1 = cmd1.getJsonData();
		
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				SendData(m_pIUserItem[i].dwUserID, sData1);
			}
		}
////////////////////////////////////
		m_vecIsKaiPaiReady[wChairID] = 1;
		int cntTanPai = 0;
		for (int i = 0; i < 4; i++) {
			if (m_vecIsKaiPaiReady[i]>0) {
				cntTanPai++;
			}
		}
		SUBCMD_S_KaiPaiReady cmd = new SUBCMD_S_KaiPaiReady();
		cmd.byRound = m_byRound;
		for (int i = 0; i < 4; i++) {
			cmd.vecIsKaiPaiReady[i] = m_vecIsKaiPaiReady[i];
		}

		String sData = cmd.getJsonData();
		int maxUser = 0;
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				SendData(m_pIUserItem[i].dwUserID, sData);
				maxUser++;
			}
		}
		if (m_bGameStarted) {
			if (cntTanPai == maxUser) {
				if (m_guize.equalsIgnoreCase("GUIZE_PT")) {
					KaiPai_PT();
				} else {
					KaiPai_HBL();
				}
				m_bGameStarted = false;
			}
		}	
		
	}
	
	public void Handle_teShuPai(Integer lUid, String[] vecArg) {

		if (vecArg.length < 2)
			return;
		int wChairID = FindSeat(lUid);
		if(m_vecIsKaiPaiReady[wChairID] == 1){
			return;
		}
		
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		int cardType = Integer.parseInt(vecArg[1]);
		m_teShuPaiList[wChairID] = cardType;
		
	
		
		m_vecIsKaiPaiReady[wChairID] = 1;
		
		
		SUBCMD_S_Teshupai cmd1 = new SUBCMD_S_Teshupai();
		cmd1.uid = lUid;
		cmd1.vecTeshupai = m_teShuPaiList;
		String sData1 = cmd1.getJsonData();
		
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				SendData(m_pIUserItem[i].dwUserID, sData1);
			}
		}
		
		SUBCMD_S_KaiPaiReady cmd = new SUBCMD_S_KaiPaiReady();
		cmd.byRound = m_byRound;
		for (int i = 0; i < 4; i++) {
			cmd.vecIsKaiPaiReady[i] = m_vecIsKaiPaiReady[i];
		}

		String sData = cmd.getJsonData();
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				SendData(m_pIUserItem[i].dwUserID, sData);
			}
		}
		
		
		
		int cntTanPai = 0;
		for (int i = 0; i < 4; i++) {
			if (m_vecIsKaiPaiReady[i]>0) {
				cntTanPai++;
			}
		}
	
		if (m_bGameStarted) {
			if (cntTanPai == m_byMaxUser) {
				if (m_guize.equalsIgnoreCase("GUIZE_PT")) {
					KaiPai_PT();
				} else {
					KaiPai_HBL();
				}
				m_bGameStarted = false;
			}
		}
		
	}
	void Handle_onExpr(int _Uid, String[] vecArg) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		int uid1 = Integer.parseInt(vecArg[1]);
		int uid2 = Integer.parseInt(vecArg[2]);
		int index = Integer.parseInt(vecArg[3]);
		
		SUBCMD_S_ExprMsg cmd = new SUBCMD_S_ExprMsg();
		cmd.m_uid1 = uid1;
		cmd.m_uid2 = uid2;
		cmd.m_index = index;
		
		SendData(Constants.INVALID_CHAIR,cmd.getJsonData());
	}
	void Handle_InBack(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
				
		SUBCMD_S_InBack cmd = new SUBCMD_S_InBack();
		cmd.byActionUser = wChairID;
		SendData(Constants.INVALID_CHAIR,cmd.getJsonData());
	}

	void Handle_InFore(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		
		SUBCMD_S_InFore cmd = new SUBCMD_S_InFore();
		cmd.byActionUser = wChairID;
		SendData(Constants.INVALID_CHAIR,cmd.getJsonData());
	}

	void Handle_onDaQiang(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		int seat1 = Integer.parseInt(vecArg[1]);
		int seat2 = Integer.parseInt(vecArg[2]);
		if(m_vecRoundDaQiangTime[seat1] == 0)
			m_vecRoundDaQiangTime[seat1]++;
		if(m_vecRoundBeiDaTime[seat2] == 0)
			m_vecRoundBeiDaTime[seat2]++;
	}

	void Handle_onQuanLeiDa(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		int seat1 = Integer.parseInt(vecArg[1]);
		if(m_vecRoundQuanLeiTime[seat1] == 0)
			m_vecRoundQuanLeiTime[seat1]++;
	}

	void Handle_GameIap(int _Uid,String vecArg[]){
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		SHMUser user = dthall.getInstance().getUserInfo(_Uid);
		if(user == null){
			return;
		}
		SUBCMD_S_GameIap cmd = new SUBCMD_S_GameIap();
		cmd.uid = _Uid;
		cmd.seat = wChairID;
		cmd.coin = user.getCoin(0);
		SendData(Constants.INVALID_CHAIR,cmd.getJsonData());
	}
	@Override
	public String getRoomInfo() {
		// TODO 自动生成的方法存根
				//m_lStartTime
				//m_pIUserItem
				// m_bTableStarted;
				// m_byRound;
				// m_byMaxRound;
				//m_dwTableID
				String roomInfo = String.format("%s/%s/%s/%s", 
						m_pIUserItem[0].GetUserInfo(), //#
						m_pIUserItem[1].GetUserInfo(), 
						m_pIUserItem[2].GetUserInfo(), 
						m_pIUserItem[3].GetUserInfo()
						);
				
				String[] vecParam = m_strParam.split(";");
				String vecParamStr = "";
				for (int i = 0; i < vecParam.length; i++) {
					String param = vecParam[i];

					if (param.equalsIgnoreCase("8JU")) {
						vecParamStr += "8局 ";
					}
					if (param.equalsIgnoreCase("16JU")) {
						vecParamStr += "16局 ";
					}

					if (param.equalsIgnoreCase("4REN"))
					{
						vecParamStr += "4人场 ";
					}
					if (param.equalsIgnoreCase( "3REN"))
					{
						vecParamStr += "3人场  ";
					}
					if (param.equalsIgnoreCase( "2REN"))
					{
						vecParamStr += "2人场  ";
					}
					if (param.equalsIgnoreCase("WUZHUANG")) {
						
						vecParamStr += "通比模式  ";
					}
					if (param.equalsIgnoreCase("DINGZHUANG")) {
						vecParamStr += "房主坐庄  ";
					}
					if (param.equalsIgnoreCase("LUNZHUANG")) {
						vecParamStr += "轮流坐庄  ";
					}
					if (param.equalsIgnoreCase("QIANGZHUANG")) {
						vecParamStr += "经典抢庄  ";
					}
					if (param.equalsIgnoreCase("GUIZE_HBL")) {
						vecParamStr += "红波浪模式";
					}
					if (param.equalsIgnoreCase("GUIZE_PT")) {
						vecParamStr += "普通模式 ";
					}
				}
				
				int start = 0;
				if(m_bTableStarted){
					start = 1;
				}
				String descInfo = String.format("十三张 %s",vecParamStr);
				roomInfo = String.format("%s|%d|%d|%d|%s|%d|%d|%d|%s|%s", roomInfo,m_dwTableID,m_lStartTime,start,descInfo,m_byMaxUser,m_byRound,m_byMaxRound,"十三张","SSZ");
				
				return roomInfo;
	}


	@Override
	public String getRoomDesc() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getPlayerCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getCostGem() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getCellScore() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getMinScore() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getStatus() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getPayway() {
		// TODO Auto-generated method stub
		return m_payWay;
	}

	@Override
	public String GetRoundInfo() {
		return "" + m_byRound + "/" +  m_byMaxRound;
	}
}
